Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to operate mongodb database with python package

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article Xiaobian for you to introduce in detail "how to use python package to operate mongodb database", the content is detailed, the steps are clear, the details are handled properly, I hope this article "how to use python package to operate mongodb database" article can help you solve your doubts, following Xiaobian's ideas slowly in depth, let's learn new knowledge.

First, install pip install pymongo II, connect to the database import pymongo# mode 1 client = pymongo.MongoClient ('mongodb://localhost:27017') # mode 2 client = pymongo.MongoClient (' localhost') 27017) # method 3, password authentication client = pymongo.MongoClient ('localhost', 27017, username='xxx') Password='xxx') third, create a database import pymongo# connection client = pymongo.MongoClient ('mongodb://localhost:27017') # create a test database db = client.test # or db = client [' test'] print (db) IV, all databases import pymongoclient = pymongo.MongoClient ('mongodb://localhost:27017') dbs = client.list_database_names () V, create a collection

That is, the table in the database

Import pymongoclient = pymongo.MongoClient ('mongodb://localhost:27017') # create test database db = client.test# create table collections = db.user # or collections = db [' user'] # delete table collections.drop () VI. Insert data

Insert_one: insert a piece of data

Insert_many: inserting multiple pieces of data

Import pymongoclient = pymongo.MongoClient ('mongodb://localhost:27017') # create the test database db = client.test# create table collections = db.user# create document data user1 = {' name': 'autofelix','age':' 255th pound: '172' user2 = {'name':' Flying Rabbit', 'age':' 288 'user2:' 182' 'weight':' 70'} # insert a document collection result = collections.insert_one (user1) print (result) print (result.inserted_id) # insert multiple document sets result = collections.insert_many ([user1, user2]) print (result) print (result.inserted_ids) 7. Query data

Find: query multiple pieces of data

Find_one: query a piece of data

Import pymongoclient = pymongo.MongoClient ('mongodb://localhost:27017') # create test database db = client.test# create table collections = db.user# query all collections.find () # query the most recent collections.find_one () # query collections.find_one ({' age':25}) 8, Advanced query import pymongoclient = pymongo.MongoClient ('mongodb://localhost:27017') # create test database db = client.test# create table collections = db.user# skip the first found data collections.find ({' age': {'$gt':10}} ['height','age']) .skip (1) # limit limit the number of queries collections.find ({' age': {'$gt':10}}, ['height','age']) .limit (1) # Multi-conditional query collections.find_one ({' height': {'$gt':150}, 'age': {' $lt':26,'$gt':10}) # in query to query data collections.find ({'age': {' $in': [25]) at the age of 25,26,32,500 26,32]}) # or query for data collections.find ({'$or': [{'age': {' $lte':23}}) whose age is less than or equal to 23 or 29. {'age': {' $gte':29}}]}) # exists query collections.find ({'age': {' $exists':True}}) # regular query collections.find ({'name': {' $regex':r'.*auto.*'}}) IX. Count statistics import pymongoclient = pymongo.MongoClient ('mongodb://localhost:27017') # create test database db = client.test# creation table collections = total number of data in the collection collections.find (). Count () # statistics on the total number of data collections.find ({' age': {'$gt':10}}). Count () 10. Modify the data

Update_one: modify a piece of data

Update_many: modifying multiple pieces of data

Import pymongoclient = pymongo.MongoClient ('mongodb://localhost:27017') # create test database db = client.test# create table collections = db.user# modify one data collections.update_one ({' name': 'autofelix'}, {' $set': {'name':' god'}) # modify multiple data collections.update_many ({'name':' autofelix'}) {'$set': {'name':' God'}) 11. Delete data

Delete_one: delete a piece of data

Delete_many: deleting multiple pieces of data

Import pymongoclient = pymongo.MongoClient ('mongodb://localhost:27017') # create test database db = client.test# create table collections = db.user# delete one data collections.delete_one ({' name': 'autofelix'}) # delete multiple data collections.delete_many ({' name': 'autofelix'}) # delete all data collections.delete_many ({}) 12. Data sort import pymongoclient = pymongo.MongoClient ('mongodb://localhost:27017') # create test database db = client.test# create table collections = db.user# sort field age in ascending order. Sort (' age') # sort field age in descending order collections.find (). Sort ('age' -1) # Multi-field sorting collections.find () .sort ('age',pymongo.ASCENDING), (' height',pymongo.ASCENDING) reads here This article "how to use python package to operate mongodb database" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about the article, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report