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