Introductory
Pymongo is currently used relatively popular a python used to connect to MongoDB Library, is the work of a variety of basic needs to meet
The specific API can refer to
Pymongo API
Pymongo GitHub
Installing MongoDB
In order to test the Pymongo connection MongoDB, the first of course need to install MONGO under CentOS, detailed reference CentOS under the installation of MongoDB
Installing Pymongo
If there is no PIP, it is recommended to install first, as to why use PIP you know
Yum Install Python-pip
PIP Installation Pymongo
Pip Install Pymongo
To see if the Pymongo installation was successful
[Email PROTECTED]/4$ Pythonpython2.7.5 (Default, Nov 20 2015, 02:00:19) [GCC4.8.5 20150623 (Red Hat 4.8.5-4)] on Linux2type" Help","Copyright","credits" or "License" forMore information.>>>ImportPymongo>>>pymongo.version'3.2.2'
using PymongoConnect to MongoDB
There are two ways of equating
>>> client = Pymongo. Mongoclient ('192.168.100.3', 27017)>>>PrintClientmongoclient (Host=['192.168.100.3:27017'], document_class=dict, Tz_aware=false, connect=True)>>> client2 = Pymongo. Mongoclient ('mongodb://192.168.100.3:27017/')>>>PrintClient2mongoclient (Host=['192.168.100.3:27017'], document_class=dict, Tz_aware=false, connect=True)>>>
get DB and collection
DB and collection are sometimes used as parameters in the script, so you can refer to the second way
Direct link
>>> db =Client.report>>>PrintDbdatabase (mongoclient (host=['192.168.100.3:27017'], document_class=dict, Tz_aware=false, connect=true), u' Report')>>> collection =Db.source_register>>>Printcollectioncollection (Database (mongoclient (host=['192.168.100.3:27017'], document_class=dict, Tz_aware=false, connect=true), u' Report'), U'Source_register')>>>
Parameter linkage, this form has two different methods
## define Parameters>>> pramadb =' Report'>>> Pramacoll ='Source_register'## Get DB>>> DB2 =CLIENT[PRAMADB]>>> DB3 =client.get_database (PRAMADB)>>>PrintDb2database (mongoclient (host=['192.168.100.3:27017'], document_class=dict, Tz_aware=false, connect=true), u' Report')>>>PrintDb3database (mongoclient (host=['192.168.100.3:27017'], document_class=dict, Tz_aware=false, connect=true), u' Report')>>>## Get Collection>>> coll2 =Db2[pramacoll]>>> Coll3 =db3.get_collection (Pramacoll)>>>Printcoll2collection (Database (mongoclient (host=['192.168.100.3:27017'], document_class=dict, Tz_aware=false, connect=true), u' Report'), U'Source_register')>>>Printcoll3collection (Database (mongoclient (host=['192.168.100.3:27017'], document_class=dict, Tz_aware=false, connect=true), u' Report'), U'Source_register')>>>
General operating Examples
## Insert or save>>> mydict = {"name":"James"," Age": 28}>>>Collection.save (mydict) ObjectId ('57fdf212bcd34f48c4fce15a')## Find>>> Collection.find ({"name":"James"}) [0]{u' Age':, U'_id': ObjectId ('57fdf212bcd34f48c4fce15a'), U'name': U'James'}>>>
Pymongo Connecting MongoDB