Python's Pymongo Usage summary

Source: Internet
Author: User
Tags mongoclient

What is Pymongo?Pymongo is a driver that enables a Python program to use a MongoDB database, written in Python. InstallationEnvironment: Ubuntu 14.04+PYTHON2.7+MONGODB 2.4first go to the official website to download the package, address click Open Link. After decompression, use python setup.py Install or install pip-m with pip Pymongo Basic use to create a connection
Import pymongoclient = Pymongo. Mongoclient (' localhost ', 27017)

Or it could be.

Import pymongoclient = mongoclient (' mongodb://localhost:27017/')
Connecting to a database

db = Client.mydb

or

db = client[' MyDB ')

Connection Aggregationaggregation is equivalent to a table in a relational database

Collection = Db.my_collection

Or

Collection = Db[' my_collection ']

View all clustered names under the database
Db.collection_names ()
Inserting records

Collection.insert ({"Key1": "Value1", "Key2", "value2"})

Deleting records

Delete all

Collection.remove ()

Delete by Condition

Collection.remove ({"Key1": "Value1"})

Update record

Collection.update ({"Key1": "Value1"}, {"$set": {"Key2": "value2", "Key3": "Value3"}})

Query recordsquery a record: Find_one () returns the first record without any parameters. Conditional lookup returns with parameters

Collection.find_one () Collection.find_one ({"Key1": "Value1"})

querying multiple records: Find () returns all records without parameters, with parameters returned by conditional lookup

Collection.find () collection.find ({"Key1": "Value1"})

View multiple records in a cluster
For item in Collection.find ():        Print Item
View the total number of clustered records
Print Collection.find (). Count ()
Query result sortsort on a single column
Collection.find (). Sort ("Key1") # Defaults to ascending Collection.find (). Sort ("Key1", Pymongo. Ascending) # Ascending collection.find (). Sort ("Key1", Pymongo. Descending) # Descending
sort on multiple columns

Collection.find (). Sort ([("Key1", Pymongo. Ascending), ("Key2", Pymongo. Descending)])






Python's Pymongo Usage summary

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.