How to Use Python to operate MongoDB database PyMongo, mongodbpymongo

Source: Internet
Author: User

How to Use Python to operate MongoDB database PyMongo, mongodbpymongo

Reference PyMongo

Copy codeThe Code is as follows:
>>> Import pymongo

Create Connection

Copy codeThe Code is as follows:
>>> Import pymongo

>>> Conn = pymongo. Connection ('localhost', 27017)

Or

Copy codeThe Code is as follows:
>>> From pymongo import Connection

>>> Conn = Connection ('localhost', 27017)

Specify the host and port parameters when creating a Connection.

Copy codeThe Code is as follows:
>>> Import pymongo
>>> Conn = pymongo. Connection (host = '2017. 0.0.1 ', port = 127)

Connect to database

Copy codeThe Code is as follows:
>>> Db = conn. ChatRoom

Or
Copy codeThe Code is as follows:
>>> Db = conn ['chatroom']

 
Connection Aggregation

Copy codeThe Code is as follows:
>>> Account = db. Account

Or
Copy codeThe Code is as follows:
>>> Account = db ["Account"]

 
View all cluster names

Copy codeThe Code is as follows:
>>> Db. collection_names ()

 
View A aggregated record

Copy codeThe Code is as follows:
>>> Db. Account. find_one ()

>>> Db. Account. find_one ({"UserName": "keyword "})

 

View aggregated Fields

Copy codeThe Code is as follows:
>>> Db. Account. find_one ({}, {"UserName": 1, "Email": 1 })
{U 'username': u 'libing ', u' _ id': ObjectId ('4ded95c3b7780a774a099b7c'), u 'email ': u 'libing @ 35.cn '}

>>> Db. Account. find_one ({}, {"UserName": 1, "Email": 1, "_ id": 0 })
{U 'username': u 'libing ', u 'email': u 'libing @ 35.cn '}

 

View multiple aggregated records

Copy codeThe Code is as follows:
>>> For item in db. Account. find ():
Item

>>> For item in db. Account. find ({"UserName": "libing "}):
Item ["UserName"]

 
View aggregated record statistics

Copy codeThe Code is as follows:
>>> Db. Account. find (). count ()

>>> Db. Account. find ({"UserName": "keyword"}). count ()

 

Sort aggregate query results

Copy codeThe Code is as follows:
>>> Db. Account. find (). sort ("UserName") -- The default value is ascending.
>>> Db. Account. find (). sort ("UserName", pymongo. ASCENDING) -- ASCENDING
>>> Db. Account. find (). sort ("UserName", pymongo. DESCENDING) -- DESCENDING

 

Multi-column sorting of clustered query results

Copy codeThe Code is as follows:
>>> Db. Account. find (). sort ([("UserName", pymongo. ASCENDING), ("Email", pymongo. DESCENDING)])

 

Add record

Copy codeThe Code is as follows:
>>> Db. Account. insert ({"AccountID": 21, "UserName": "libing "})

 

Modify record

Copy codeThe Code is as follows:
>>> Db. account. update ({"UserName": "libing" },{ "$ set": {"Email": "libing@126.com", "Password": "123 "}})

 

Delete record

Copy codeThe Code is as follows:
>>> Db. Account. remove () -- delete all

>>> Db. Test. remove ({"UserName": "keyword "})

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.