Python operates mongodb in nine steps
- 2020-10-31 21:48:15
- OfStack
1 the import pymongo
from pymongo import MongoClient
]
Connect to server port number 27017
Connection MongoDB
To connect to MongoDB, we need to use MongoClient in the PyMongo library. Generally, we can pass in IP and port of MongoDB. The first parameter is address host, and the second parameter is port port.
[conn = MongoClient("localhost")
MongoClient(host='127.0.0.1',port=27017)
3 Connect to the database
db = conn. database name
Connection collection
[collection = db[collection_name]
or
collection = db.collection_name
View all aggregate names
[db.collection_names()
]
4 Inserting Data
(1) Insert 1 piece of data
db. user. insert ({" name ":" xiali just ", "age" : 18, "hobby" : "learning"})
](2) Insert multiple pieces of data
[db. user. insert ([{" name ":" xiali just ", "age" : 18, "hobby" : "learning"}, {" name ":" xxxoo ", "age" : 48, "hobby" : "learning"}]
](3) Above x is recommended
[insert_one inserts 1 data
insert_many() inserts multiple pieces of data
](4) Return id use insert_one()
[data.inserted_id
data.inserted_ids
]
5 Query data
(1) Query all
db.user.find()
[
# Conditional queries
# data = ES126en.user.find ({"name":" Sunday "})
# print(data) # returns result similar to 1 iterator which can be pulled out one by one using the next method
# print(next(data)) # extract 1 piece of data
2) Query 1 item
[db.user.find_one()
](3) Conditional query
[db. user. find ({" name ":" 3 "})
](4) query id
[from bson.objectid import ObjectId*# for ID queries
data = db.user.find({"_id":ObjectId("59a2d304b961661b209f8da1")})
](5) Fuzzy query
[(1) {"name":{'$regex':" Zhang "}}
(2) import re {'xxx':re.compile('xxx')}
6 sort limit count skip
(1) sort sorting
The & # 8203; Age > 10
[data = db. user.find({"age":{"$gt":10}).sort("age",-1) # age ascending query pymongo. ASCENDING -- ascending
data = db.user.find({"age":{"$gt":10}).sort("age",1) # age descending order query pymongo.DESCENDING -- descending order
(2) limit values
The & # 8203; Take 3 pieces of data
[db.user.find().limit(3)
data = db.user.find({"age":{"$gt":10}}).sort("age",-1).limit(3)
](3) Number of count statistics
db.user.find().count()
(4) skip starts from the number of data
db.user.find().skip(2)
7 update modify
The & # 8203; The update() method is also officially not recommended. The update_one() method and update_many() method are also divided here. The usage is more strict.
(1) update()
[db. user. update ({" name ":" 3 "}, {" $set ": {" age" : 25}})
db. user. update ({" name ":" 3 "}, {" $inc ": {" age" : 25}})
(2) update_one() Update the data that meets the conditions in article 1
[The & # 8203; db. user. update_one ({" name ":" 3 "}, {" $set ": {" age" : 99}})
](3) update_many() updates all eligible data
[db. user. update_many ({" name ":" 3 "}, {" $set ": {" age" : 91}})
](4) The returned result is type UpdateResult, and then call the attributes matched_count and modified_count to get the number of matching data and the number of affected data, respectively.
print (result matched_count, result modified_count)
8 remove delete
The deletion operation is relatively simple, just call remove() method to specify the deletion condition, and all data that meets the criteria will be deleted.
(1) Delete sheet 3
[collection.remove({"name":"lilei"})
](2) Delete all
[collection.remove()
](3) There are still two new recommended methods, delete_one() and delete_many(). Examples are as follows:
[delete_one() deletes data that meets the criteria in article 1
collection. delete_one ({" name ":" Kevin "})
delete_many() deletes all eligible data and returns a result of type DeleteResult
collection. delete_many ({" age ": {$lt: 25}})
](4) The number of deleted data bars can be obtained by calling deleted_count attribute.
[result.deleted_count
]
9 Close connection
conn.close()