This article shares with you the detailed steps and example codes for operating mongodb in Python. Friends in need can refer to it.
1 Import pymongo
1 | from pymongo import MongoClient
|
Copy after login
2 Connect to the server port number 27017
Connect to MongoDB
To connect to MongoDB we need to use the MongoClient in the PyMongo library. Generally speaking, just pass in the IP and port of MongoDB. The first parameter is the address host, and the second parameter is the port. If the port is not passed, the default is 27017.
1 2 | conn = MongoClient( "localhost" )
MongoClient(host='127.0.0.1',port=27017)
|
Copy after login
Three connection database
db = conn.Database name
Connection collection
1 2 3 | collection = db[collection_name]
or
collection = db.collection_name
|
Copy after login
View all collection names
Four Insert Data
(1) Insert one piece of data
1 | db.user.insert({ "name" : "夏利刚" , "age" :18, "hobby" : "学习" })
|
Copy after login
(2) Insert multiple pieces of data
1 | db.user.insert([{ "name" : "夏利刚" , "age" :18, "hobby" : "学习" },{ "name" : "xxxoo" , "age" :48, "hobby" : "学习" }]
|
Copy after login
(3) It is recommended to use
1 2 | insert_one 插入一条数据
insert_many() 插入多条数据
|
Copy after login
## on 3.x and above #(4) Return id and use insert_one()
1 2 | data.inserted_id
data.inserted_ids
|
Copy after login
五query data
(1) Query all
db.user.find()
1 2 3 4 | #带条件的查询
# data = db.user.find({ "name" : "周日" })
# print (data) #返回result类似一个迭代器 可以使用 next方法 一个一个 的取出来
# print (next(data)) #取出一条数据
|
Copy after login
2) Query a
(3) Query with conditions
1 | db.user.find({ "name" : "张三" })
|
Copy after login
(4) Query id
1 2 | from bson.objectid import ObjectId*#用于ID查询
data = db.user.find({ "_id" :ObjectId( "59a2d304b961661b209f8da1" )})
|
Copy after login
(5) Fuzzy query
1 2 | (1){ "name" :{' $regex ': "张" }}
(2)import re {'xxx':re.compile('xxx')}
|
Copy after login
Six sort limit count skip
(1) sort sort
age is greater than 10
1 2 | data = db.user.find({ "age" :{ "$gt" :10}}).sort( "age" ,-1) #年龄 升序 查询 pymongo.ASCENDING --升序
data = db.user.find({ "age" :{ "$gt" :10}}).sort( "age" ,1) #年龄 降序 查询 pymongo.DESCENDING --降序
|
Copy after login
(2) limit value
take three pieces of data
1 2 | db.user.find().limit(3)
data = db.user.find({ "age" :{ "$gt" :10}}).sort( "age" ,-1).limit(3)
|
Copy after login
(3) count The number of statistical data items
db.user.find().count()
(4) skip From which piece of data to start
db.user.find ().skip(2)
seven update modification
The update() method is actually a method that is not officially recommended. Here it is also divided into update_one() method and update_many() method. The usage is more strict,
(1) update()
1 2 | db.user.update({ "name" : "张三" },{ "$set" :{ "age" :25}})
db.user.update({ "name" : "张三" },{ "$inc" :{ "age" :25}})
|
Copy after login
(2) update_one() Update the first qualifying data
1 | db.user.update_one({ "name" : "张三" },{ "$set" :{ "age" :99}})
|
Copy after login
(3) update_many() Update all qualifying data
1 | db.user.update_many({ "name" : "张三" },{ "$set" :{ "age" :91}})
|
Copy after login
(4) Its return result It is an UpdateResult type, and then call the matched_count and modified_count attributes to get the number of matching data and the number of affected data respectively.
print(result.matched_count, result.modified_count) No
eight remove Delete
The deletion operation is relatively simple. Directly call the remove() method to specify the conditions for deletion. That’s it, all data that meets the conditions will be deleted,
(1) Delete Zhang San
1 | collection.remove({ "name" : "lilei" })
|
Copy after login
collection.remove({"name":"lilei"})
(2) Delete all
(3) There are still two new recommended methods, delete_one() and delete_many() methods. The examples are as follows:
1 2 3 4 | delete_one()即删除第一条符合条件的数据
collection.delete_one({“name”:“ Kevin”})
delete_many()即删除所有符合条件的数据,返回结果是DeleteResult类型
collection.delete_many({“age”: { $lt :25}})
|
Copy after login
(4) You can call the deleted_count attribute to get the deleted data strips number.
Nine close connections
Related recommendations:
How to operate MongoDB with PHP and simple analysis
The above is the detailed content of 9 steps to operate mongodb with Python. For more information, please follow other related articles on the PHP Chinese website!