Reference Documentation:
1, https://docs.mongodb.org/getting-started/python/
2, http://api.mongodb.org/python/current/api/pymongo/index.html
#-*-coding:utf-8-*-ImportSYS fromDatetimeImportdatetime fromPymongoImportmongoclientImportPymongoImportRedefMain ():" "Connect to database. Client = Mongoclient () is equivalent to client = Mongoclient ("localhost:27017")" "Client=mongoclient () DB=Client.demo Coll=db.address" "Insert Data. Insert_one () Insert_many ( )" "Insertresult=Coll.insert_one ({" Account":"[email protected]", "Password":"123456", "Birth": Datetime.strptime ("1990-09-22","%y-%m-%d"), "score": 102, "Address": {" City": ["Beijing","Wuxi","Chengdu"],"Country":" China"}, "Articles": [{"title":"Summary of common operation of MONGO","author":"Xiaomaque"}, {"title":"MySQL Summary","author":"ponpon7"}] }) Print(insertresult.inserted_id) elements= [{" Account":"ponpon7","Password":"Afi89^^r","Birth": Datetime.strptime ("1986-12-12","%y-%m-%d"),"score": 172}, {" Account":"Liudehua","Password":"Wangqingshui","Birth":"","score": 18}] Coll.insert_many (elements)" "Find or query data. Cursorall: Find score<140 data, and sort by account from small to large, score from large to small. $lt: Less than<; $GT: Greater than> cursoror:logical OR Cursorregex: Finding by regular expression" "Cursorall= Coll.find ({"score": {"$lt": 140}}). sort ([(" Account", Pymongo. Ascending), ("score", Pymongo. Descending)]) forDocumentinchCursorall:Print("All document: {}". Format (document)) Cursorregex= Coll.find ({" Account": Re.compile ('P', Re. I)}) forDocumentinchCursorregex:Print("Regex document: {}". Format (document)) Cursoror= Coll.find ({"$or": [{"score": 172}, {"Address.country":" China"}]}) forDocumentinchCursoror:Print("score is 172 Or country are China: {}". Format (document))" "Update data & replace data. Update_One (filter, UPDATE, Upsert=false, Bypass_document_validation=false) Upsert: If True, data is inserted when a document that matches the filter is not found , the default is False" "Updateresult=Coll.update_many ({" Account":"[email protected]"}, { "$set": {"score": 129,"Address": {" City": ["Nanjing"],"Country":" China"}}, "$currentDate": {"lastmodified": True},#True ) Print("Update:matched_count is {}". Format (updateresult.matched_count))Print("Update:modified_count is {}". Format (updateresult.modified_count)) Replaceresult=Coll.replace_one ({" Account":"[email protected]"}, { " Account":"[email protected]", "score": 139, "Address": {" City": ["Paris"],"Country":"France"}, } ) Print("Replace:matched_count is {}". Format (replaceresult.matched_count))Print("Replace:modified_count is {}". Format (replaceresult.modified_count))" "remove Data. Attintion:delete_many ({}) Delete all documents Coll.drop () drop a collection" "Deleteresult= Coll.delete_many ({"Birth": Datetime.strptime ("1990-09-22","%y-%m-%d")}) Print("Remove:deleted_count is {}". Format (deleteresult.deleted_count))if __name__=='__main__': Main ()
Pymongo study 1th--additions and deletions