The Pymongo of MongoDB

Source: Internet
Author: User
Tags mongoclient

Pymongo is what Pymongo is a driver that enables Python programs to be written using a MongoDB database using Python. Installation environment: Ubuntu 14.04+PYTHON2.7+MONGODB 2.4 Go to the official website to download the package, address click Open Link. After decompression, enter, use python setup.py Install  To install or install with PIP pip-m Install PymongoBasic use to create a connection
    1. Import Pymongo
    2. Client = Pymongo. Mongoclient (' localhost ', 27017)

Or it could be.

    1. Import Pymongo
    2. Client = mongoclient (' mongodb://localhost:27017/')
Connecting to a database
    1. db = Client.mydb
Or
    1. db = client[' mydb ')
Connecting clustered aggregates equivalent to tables in relational databases
    1. Collection = Db.my_collection
Or
    1. Collection = db[' my_collection ']
View all clustered names under the database
    1. Db.collection_names ()
Inserting records
    1. Collection.insert ({"Key1":"value1","Key2","value2"})
Deleting records

Delete all

    1. Collection.remove ()
Delete by condition
    1. Collection.remove ({"Key1":"value1"})
Update record
    1. Collection.update ({"Key1": "value1"}, {"$set": {"Key2": "value2", "Key3": "Value3"}})
Query records query a record: Find_one () returns the first record without any parameters. Conditional lookup returns with parameters
    1. Collection.find_one ()
    2. Collection.find_one ({"Key1":"value1"})
Querying multiple records: Find () returns all records without parameters, with parameters returned by conditional lookup
    1. Collection.find ()
    1. Collection.find ({"Key1":"value1"})
View multiple records in a cluster  
    1. For item in Collection.find ():
    2. Print Item
View the total number of clustered records
    1. Print Collection.find (). Count ()
Sort the query Result sort column
    1. Collection.find (). Sort ("Key1") # Default to Ascending
    2. Collection.find (). Sort ("Key1", Pymongo. Ascending) # ascending
    3. Collection.find (). Sort ("Key1", Pymongo. Descending) # descending
Sort on multiple columns
    1. Collection.find (). Sort ([("Key1", Pymongo. Ascending), ("Key2", Pymongo. Descending)])

The Pymongo of 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.