Python Operations MongoDB database Pymongo Library using methods _python

Source: Internet
Author: User

Referencing Pymongo

Copy Code code as follows:

>>> Import Pymongo

Create Connection Connection

Copy Code code as follows:

>>> Import Pymongo

>>> conn = Pymongo. Connection (' localhost ', 27017)


Or

Copy Code code as follows:

>>> from Pymongo import Connection

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


When creating connection, specify host and port parameters

Copy Code code as follows:

>>> Import Pymongo
>>> conn = Pymongo. Connection (host= ' 127.0.0.1 ', port=27017)

Connecting to a database

Copy Code code as follows:

>>> DB = conn. Chatroom

Or
Copy Code code as follows:

>>> db = conn[' chatroom ']


Connection Aggregation

Copy Code code as follows:

>>> account = db. Account

Or
Copy Code code as follows:

>>> account = db[' account ']


View all clustered names

Copy Code code as follows:

>>> Db.collection_names ()


view a single record of a cluster

Copy Code code as follows:

>>> db. Account.find_one ()

>>> db. Account.find_one ({"UserName": "Keyword"})


To view a clustered field

Copy Code code 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 records of a cluster

Copy Code code as follows:

>>> for item in DB. Account.find ():
Item

>>> for item in DB. Account.find ({"UserName": "Libing"}):
item["UserName"]



to view aggregated record statistics

Copy Code code as follows:

>>> db. Account.find (). Count ()

>>> db. Account.find ({"UserName": "Keyword"}). Count ()


Clustered query Result Ordering

Copy Code code as follows:

>>> db. Account.find (). Sort ("UserName")--default to Ascending
>>> db. Account.find (). Sort ("UserName", Pymongo). Ascending)--Ascending
>>> db. Account.find (). Sort ("UserName", Pymongo). Descending)--descending

Clustered query results multiple-column sorting

Copy Code code as follows:

>>> db. Account.find (). Sort ([("UserName", Pymongo. Ascending), ("Email", Pymongo. Descending)])

Add a record

Copy Code code as follows:

>>> db. Account.insert ({"AccountId": "UserName": "Libing"})

Modify Records

Copy Code code as follows:

>>> db. Account.update ({"UserName": "Libing"},{"$set": {"Email": "Libing@126.com", "Password": "123"})

Delete a record

Copy Code code as follows:

>>> db. Account.remove ()--Delete all

>>> db. Test.remove ({"UserName": "Keyword"})

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.