Tutorial on using Python scripts to operate MongoDB, pythonmongodb
Connect to database
Dedicated client VS Connection
class MongoClient(pymongo.common.BaseObject) | Connection to MongoDB. | | Method resolution order: | MongoClient | pymongo.common.BaseObject | __builtin__.object |class Connection(pymongo.mongo_client.MongoClient) | Connection to MongoDB. | | Method resolution order: | Connection | pymongo.mongo_client.MongoClient | pymongo.common.BaseObject | __builtin__.object
From the inheritance of the two classes, connection inherits the established client. We recommend that you use the established client instead of the Connection. (That is, the client can use both methods of Connection)
from pymongo import MongoClientclient = MongoClient('192.168.40.87', 27037)db_name = 'TCL_Useraction'db = client[db_name]collection_useraction = db['useraction']
In this example, you can access the database and set through the dictionary, and you can also access the database by using the. (DOT) method.
Insert data
Save () VS insert ()
Both the save and insert functions of mongodb can insert data into the collection, but there are two differences:
1. the save Function actually calls the insert or update function based on the parameter conditions. if the data object to be inserted exists, the insert function reports an error, while the save function changes the original object. If the objects to be inserted do not exist, they perform the same insert operation. here we can use a few words to summarize the differences between the two, that is, the so-called "Yes, but no ".
2. insert can insert a list at a time without traversing, which is highly efficient. save needs to traverse the list and insert one by one.
Update Data
For a single data, you can use the save method after updating it.
Update (criteria, objNew, upsert, mult)
Criteria: conditional expression to be updated
ObjNew: Update expression
Upsert: whether to insert a new document if the target record does not exist.
Multi: whether to update multiple documents.
collection_useraction.update({'gid':last_gid, 'time':l_date}, {'$set':{'gid':last_gid}, '$set':{'time':l_date}, '$addToSet':{'categories':category_data}}, upsert=True)
Delete data
Db. users. drop () # delete a collection remove (self, spec_or_id = None, safe = None, multi = True, ** kwargs) # remove () is used to delete a single or all documents, deleted documents cannot be recovered. Id = db. users. find_one ({"name": "user2"}) ["_ id"] db. users. remove (id) # delete a record db Based on the id. users. remove () # delete all records in the database. users. remove ({'yy': 5}) # delete records with yy = 5
Query
5. Query
# Query for u in db. users. find ({"age": {"$ lt": 15}) with age less than 15: print u
5.1 query a record
# Query for u in db. users. find ({"name": "user8"}): print u
# Obtain a query u2 = db. users. find_one ({"name": "user9"}) # If no query result is found, None print u2 is returned.
5.2 query a specific key (fields)
# select name, age from users where age = 21 for u in db.users.find({"age":21}, ["name", "age"]): print u for u in db.users.find(fields = ["name", "age"]): print u
5.3 sorting (SORT)
Pymongo. ASCENDING # You can also use 1 to replace pymongo. DESCENDING # You can also use-1 to replace for u in db. users. find (). sort ([("age", pymongo. ASCENDING)]): print u # select * from set name order by key 1 for u in db. users. find (). sort ([("age", pymongo. DESCENDING)]): print u # select * from set name order by key 1 desc for u in db. users. find (). sort ([("Key 1", pymongo. ASCENDING), ("Key 2", pymongo. DESCENDING)]): print u # select * from set name order by key 1 asc, key 2 desc for u in db. users. find (sort = [("Key 1", pymongo. ASCENDING), ("Key 2", pymongo. DESCENDING)]): another method of print u # sort for u in db. users. find ({"name": "user9"}, sort = [['name', 1], ['sex', 1], fields = ["name ", "age", 'sex']): print u # combination
5.4 Number of lines read from SLICE (LIMIT)
# Select * from set name skip 2 limit 3 # MySQL statement: select * from set name limit 2, 3 for u in db. users. find (). skip (2 ). limit (3): print u for u in db. users. find (skip = 2, limit = 3): print u # You can use slice instead of skip & limit ($ slice in mongo seems a bit problematic ). For u in db. users. find () [2: 5]: print u # write for u in db separately. users. find (). skip (2): print u for u in db. users. find (skip = 1): print u for u in db. users. find (). limit (5): print u for u in db. users. find (limit = 3): print u
5.5 Multi-condition query (Conditional Operators) # regular expressions can be used for like queries
# select * from users where name = 'user3' and age > 12 and age < 15 for u in db.users.find({'age': {'$gt': 12, '$lt': 15}, 'name': 'user3'}): print u # select * from users where name = 'user1' and age = 21 for u in db.users.find({"age":21, "name":"user1"}): print u
5.6 IN
for u in db.users.find({"age":{"$in":(23, 26, 32)}}): print u # select * from users where age in (23, 26, 32) for u in db.users.find({"age":{"$nin":(23, 26, 32)}}): print u # select * from users where age not in (23, 26, 32)
5.7 COUNT)
print(db.users.count()) # select count(*) from users print(db.users.find({"age":{"$gt":30}}).count()) # select count(*) from users where age > 30
5.8 OR
For u in db. users. find ({"$ or": [{"age": 25 },{ "age": 28}]}): print u # select * from set name where key 1 = value 1 or key 1 = value 2 for u in db. users. find ({"$ or": [{"age": {"$ lte": 23 }}, {"age": {"$ gte ": 33 }}]}): print u # select * from set name where key 1 <= value 1 or key 1> = value 2
6. exists)
Db. users. find ({'sex': {'$ exists': True}) # select * from set name where exists key 1 db. users. find ({'sex': {'$ exists': False}) # select * from set name where not exists key 1
7. Regular Expression Query
For u in db. users. find ({"name": {"$ regex": r "(? I) user [135] "}}, [" name "]): print u # the query results show that the name is user1, user3, and user5.
8. Multi-level path element value matching
Document adopts the hierarchical structure of JSON-like, so we can directly use embedded (Embed) to replace the Reference of traditional relational databases ).
MongoDB supports the namespace path separated by ".". The multilevel path in the condition expression must be enclosed in quotation marks.
# If the key contains an array, you can simply query the database by matching whether the array attribute contains this element. set Name. find_one ({'address': "address1"}) # address is an array. You only need to include it when matching. # The query result is {"_ id ": objectId ("4c479885089df9b53474170a"), "name": "user1", "address": ["address1", "address2"]} # The multilevel path in the condition expression must be enclosed in quotation marks, to ". "Split u = db. set Name. find_one ({"im. qq ": 12345678}) # The query result is: {" _ id ": ObjectId (" 4c479885089df9b51_1_a ")," name ":" user1 "," im ": {"msn": "user1@hotmail.com", "qq": 12345678} print u ['im '] ['msn'] # display: user1@hotmail.com # multi-level path update db. set Name. update ({"im. qq ": 12345678}, {'$ set': {" im. qq ": 12345}) # query for u in db with specific keys. users. find ({"im. qq ": {'$ exists': True }}, {" im. qq ": 1}): print u # display example: {" _ id ": ObjectId (" 4c479885089df9b54254170a ")," im ": {" qq ": 12345} for u in db. users. find ({'data': "abc"}): print u #: {"_ id": ObjectId ("4c47a481b48cde79c6780df5"), "name": "user8 ", "data": [{"a": 1, "B": 10}, 3, "abc"]} for u in db. users. find ({'data': {'$ elemmatch': {'A': 1,' B ': {' $ gt ': 5 }}}}): print u #: {"_ id": ObjectId ("4c47a481b48cde79c6780df5"), "name": "user8", "data": [{"a": 1, "B": 10}, 3, "abc"]}
{Data: "abc"} simply matches whether the array attribute contains this element. $ ElemMatch can process more complex element search conditions. You can also write it as follows:
Db. Set Name. find ({"data. a": 1, "data. B": {'$ gt': 5 }})
You can also use serial numbers for operations on Arrays:
Db. Set Name. find ({"data.1": 3}) # sequence number starts from 0
# For example, {"classifyid": "test1", "keyword": [{"name": 'test1 ', # change this value to test5 (array subscript starts from 0, subscript is also used) "frequence": 21 ,}, {"name": 'test2 ', # The Sub-Table query matches the value "frequence": 50 ,},] }# modify the sub-table (the other content of the sub-table remains unchanged) db. set Name. update ({"classifyid": "test1"}, {"$ set": {"keyword.0.name": 'test5'}) # query the database of the subtable. set Name. find ({"classifyid": "test1", "keyword.0.name": "test2 "})
Operation
(1) $ all: determines whether the array attribute contains all conditions.
Db. users. insert ({'name': "user3", 'data': [1, 2, 3, 4, 5, 6, 7]}) db. users. insert ({'name': "user4", 'data': [1, 2, 3]}) for u in db. users. find ({'data': {'$ all': [2, 3, 4]}): print u # display: {"_ id": ObjectId ("4c47a133b48cde79c6780df0 "), "name": "user3", "data": [1, 2, 3, 4, 5, 6, 7]}
Pay attention to the difference with $ in. $ In indicates that the target attribute value is a member of the condition expression, while $ all indicates that the attribute value contains all the condition elements.
(2) $ size: number of elements matching the array attribute.
For u in db. users. find ({'data': {'$ size': 3}): print u # only show: {"_ id" that matches the number of this array ": objectId ("4c47a13bb48cde79c6780df1"), "name": "user4", "data": [1, 2, 3]}
(3) $ type: determines the attribute type.
For u in db. users. find ({'T': {'$ type': 1}): print u # query for u in db. users. find ({'T': {'$ type': 2}): print u # query string type
Type value:
Double: 1
String: 2
Object: 3
Array: 4
Binary data: 5
Object id: 7
Boolean: 8
Date: 9
Null: 10
Regular expression: 11
Javascript code: 13
Symbol: 14
Javascript code with scope: 15
32-bit integer: 16
Timestamp: 17
64-bit integer: 18
Min key: 255
Max keys: 127
(4) $ not: indicates a document with invalid return conditions.
It seems that it can only be used with regular expressions and $ mod ????
# Do not know how to use
(5) $ unset: opposite to $ set, which indicates removing document attributes.
For u in db. users. find ({'name': "user1"}): print u #, for example, {"_ id": ObjectId ("4c479885089df9b53474170a"), "name": "user1 ", "age": 15, "address": ["address1", "address2"]} db. users. update ({'name': "user1"}, {'$ unset': {'address': 1, 'age': 1}) for u in db. users. find ({'name': "user1"}): print u #, for example, {"_ id": ObjectId ("4c479885089df9b53474170a"), "name": "user1 "}
(6) $ push: and $ pushAll both add elements to the array attribute. # It seems that there is no difference between the two
For u in db. users. find ({'name': "user1"}): print u #, for example, {"_ id": ObjectId ("4c479885089df9b53474170a"), "age": 15, "name": "user1"} db. users. update ({'name': "user1"}, {'$ push': {'data': 1}) for u in db. users. find ({'name': "user1"}): print u #, for example, {"_ id": ObjectId ("4c479885089df9b53474170a"), "age": 15, "data": [1], "name": "user1"} db. users. update ({'name': "user1"}, {'$ pushAll': {'data': [2, 3, 4, 5]}) for u in db. users. find ({'name': "user1"}): print u #, for example, {"_ id": ObjectId ("4c479885089df9b53474170a"), "age": 15, "data": [1, 2, 3, 4, 5], "name": "user1 "}
(7) $ addToSet: similar to $ push, but added only when the element does not exist (Set indicates a Set of non-repeating elements ).
Db. users. update ({'name': "user2" },{ '$ unset': {'data': 1}) db. users. update ({'name': "user2" },{ '$ addtoset': {'data': 1}) db. users. update ({'name': "user2" },{ '$ addtoset': {'data': 1}) for u in db. users. find ({'name': "user2"}): print u # display: {"_ id": ObjectId ("4c479896089df9b51_1_ B"), "data": [1], "name": "user2"} db. users. update ({'name': "user2"}, {'$ push': {'data': 1}) for u in db. users. find ({'name ': "User2"}): print u # display: {"_ id": ObjectId ("4c479896089df9b51_1_ B"), "data": [1, 1], "name ": "user2"} to add multiple elements, use $ each. Db. users. update ({'name': "user2"}, {'$ addtoset': {'data': {' $ each ': [1, 2, 4]}) for u in db. users. find ({'name': "user2"}): print u # display: {u'age': 12, U' _ id': ObjectId ('4c479896089df9b51_1_ B '), u'data': [1, 1, 2, 3, 4], u'name': u'user2'} # It seems that duplicate entries will not be deleted automatically.
(8) $ each to add multiple elements.
Db. users. update ({'name': "user2" },{ '$ unset': {'data': 1}) db. users. update ({'name': "user2" },{ '$ addtoset': {'data': 1}) for u in db. users. find ({'name': "user2"}): print u # display: {"_ id": ObjectId ("4c479896089df9b51_1_ B"), "data": [1], "name": "user2"} db. users. update ({'name': "user2"}, {'$ addtoset': {'data': {' $ each ': [1, 2, 4]}) for u in db. users. find ({'name': "user2"}): print u # display: {u'age': 12, U' _ id': ObjectId ('4c479896089df9b51_1_ B '), u'data': [1, 2, 3, 4], u'name': u'user2'} db. users. update ({'name': "user2"}, {'$ addtoset': {'data': [1, 2, 4]}) for u in db. users. find ({'name': "user2"}): print u # display: {"_ id": ObjectId ("4c479896089df9b51_1_ B"), "data": [1, 2, 3, 4, [1, 2, 3, 4], "name": "user2"} db. users. update ({'name': "user2" },{ '$ unset': {'data': 1}) db. users. update ({'name': "user2"}, {'$ addtoset': {'data': [1, 2, 4]}) for u in db. users. find ({'name': "user2"}): print u # display: {"_ id": ObjectId ("4c47a133b48cde79c6780df0"), "data": [[1, 2, 3, 4], "name": "user2 "}
(9) $ pop: Removes elements from the array attribute (by array subscript), $ pull removes by value, and $ pullAll removes all elements that match the submission.
Db. users. update ({'name': "user2" },{ '$ unset': {'data': 1}) db. users. update ({'name': "user2"}, {'$ addtoset': {'data': {' $ each ': [1, 2, 3, 4, 5, 6, 7, 2, 3] }}) for u in db. users. find ({'name': "user2"}): print u # display: {"_ id": ObjectId ("4c47a133b48cde79c6780df0"), "data": [1, 2, 3, 4, 5, 6, 7, 2, 3], "name": "user2"} db. users. update ({'name': "user2" },{ '$ pop': {'data': 1 }}) # remove the last element for u in db. users. find ({'name': "user2"}): print u # display: {"_ id": ObjectId ("4c47a133b48cde79c6780df0"), "data": [1, 2, 3, 4, 5, 6, 7, 2], "name": "user2"} db. users. update ({'name': "user2" },{ '$ pop': {'data':-1}) # Remove the first element for u in db. users. find ({'name': "user2"}): print u # display: {"_ id": ObjectId ("4c47a133b48cde79c6780df0"), "data": [2, 3, 4, 5, 6, 7, 2], "name": "user2"} db. users. update ({'name': "user2" },{ '$ pull': {'data': 2 }}) # Remove all 2 for u in db. users. find ({'name': "user2"}): print u # display: {"_ id": ObjectId ("4c47a133b48cde79c6780df0"), "data": [3, 4, 5, 6, 7], "name": "user2"} db. users. update ({'name': "user2"}, {'$ pullall': {'data': [, 6]}) # Remove, 6 for u in db. users. find ({'name': "user2"}): print u # display: {"_ id": ObjectId ("4c47a133b48cde79c6780df0"), "data": [4, 7], "name": "user2 "}
(10) $ where: Use JS Code to replace some ugly $ lt and $ gt.
MongoDB has built-in Javascript Engine (SpiderMonkey ). Javascript expressions can be used directly, or even JavaScript functions can be used to write more complex Code blocks.
Db. users. remove () # delete all records in the set for I in range (10): db. users. insert ({'name': "user" + str (I), 'age': I}) for u in db. users. find (): print u #: {"_ id": ObjectId ("4c47b3372a9b2be866da226e"), "name": "user0", "age ": 0 }{ "_ id": ObjectId ("4c47b3372a9b2be866da226f"), "name": "user1", "age": 1 }{ "_ id ": objectId ("4c47b3372a9b2be866da2270"), "name": "user2", "age": 2 }{ "_ id": ObjectId ("4c47b3372a9b2be866da2271"), "name ": "user3", "age": 3 }{ "_ id": ObjectId ("4c47b3372a9b2be866da2272"), "name": "user4", "age ": 4} {"_ id": ObjectId ("4c47b3372a9b2be866da2273"), "name": "user5", "age": 5 }{ "_ id ": objectId ("4c47b3372a9b2be866da2274"), "name": "user6", "age": 6 }{ "_ id": ObjectId ("4c47b3372a9b2be866da2275"), "name ": "user7", "age": 7 }{ "_ id": ObjectId ("4c47b3372a9b2be866da2276"), "name": "user8", "age ": 8} {"_ id": ObjectId ("4c47b3372a9b2be866da2277"), "name": "user9", "age": 9} for u in db. users. find ({"$ where": "this. age> 7 | this. age <3 "}): print u #: {u 'age': 0.0, U' _ id': ObjectId ('4c47b3372a9b2be866da226e '), u 'name ': u'user0'} {u'age': 1.0, U' _ id': ObjectId ('4c47b3372a9b2be866da226f'), u 'name ': u'user1'} {u'age': 2.0, U' _ id': ObjectId ('4c47b3372a9b2be866da2270 '), u'name ': u'user2'} {u'age': 8.0, U' _ id': ObjectId ('4c47b3372a9b2be866da2276 '), u 'name ': u'user8'} {u'age': 9.0, U' _ id': ObjectId ('4c47b3372a9b2be866da2277 '), u 'name ': u'user9'} for u in db. users. find (). where ("this. age> 7 | this. age <3 "): print u #: {u 'age': 0.0, U' _ id': ObjectId ('4c47b3372a9b2be866da226e '), u 'name ': u'user0'} {u'age': 1.0, U' _ id': ObjectId ('4c47b3372a9b2be866da226f'), u 'name ': u'user1'} {u'age': 2.0, U' _ id': ObjectId ('4c47b3372a9b2be866da2270 '), u'name ': u'user2'} {u'age': 8.0, U' _ id': ObjectId ('4c47b3372a9b2be866da2276 '), u 'name ': u'user8'} {u'age': 9.0, U' _ id': ObjectId ('4c47b3372a9b2be866da2277 '), u 'name ': u'user9'} # Use the custom function, javascript syntax for u in db. users. find (). where ("function () {return this. age> 7 | this. age <3;} "): print u #: {u 'age': 0.0, U' _ id': ObjectId ('4c47b3372a9b2be866da226e '), u 'name ': u'user0'} {u'age': 1.0, U' _ id': ObjectId ('4c47b3372a9b2be866da226f'), u 'name ': u'user1'} {u'age': 2.0, U' _ id': ObjectId ('4c47b3372a9b2be866da2270 '), u'name ': u'user2'} {u'age': 8.0, U' _ id': ObjectId ('4c47b3372a9b2be866da2276 '), u 'name ': u'user8'} {u'age': 9.0, U' _ id': ObjectId ('4c47b3372a9b2be866da2277 '), u 'name': u 'user9 '}