Pymongo Connecting MongoDB

Source: Internet
Author: User
Tags db2 mongoclient mongodb

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.