Online about Python operation MongoDB related articles are relatively not many, and the quality is not very high! The following gives a complete delete and change to check the sample program!
#!/usr/bin/python#-*-coding:utf-8-*-ImportPymongoImportreconnection= Pymongo. Mongoclient ('10.38.164.80', 27017) TDB=connection.testcollection=tdb.article#Inserting DataTry: Insert_data={"ID":"2","value":"ABC"} collection.insert (Insert_data)exceptbaseexception:Print "Insert Exception"#Querying DataTry: PrintCollection.find_one ({"ID":"2"}) Cursor= Collection.find ({"title": Re.compile ("^. {0,50} (goddess)")},{"title": 1,"URL": 1}) forResultinchcursor:PrintType (Result)#View Type PrintSTR (result). Decode ("Unicode-escape") PrintResult.get ("title") #Print str (result). Decode (' Unicode_escape ')exceptbaseexception,e:Print "Querying data Exceptions"+Str (e)#Modifying DataTry: Collection.update ({"ID":"2"},{"$set":{"value":"123"}})exceptbaseexception,e:Print "failed to update data" Printe#Delete DataTry: Collection.remove ({"ID":"2"})exceptbaseexception,e:Print "Delete Data Exception" PrintE
To install the MONGO library, install the following command (Ubuntu): Pip Install Pymongo
Python Operation MongoDB Database