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
- Import Pymongo
- Client = Pymongo. Mongoclient (' localhost ', 27017)
Or it could be.
- Import Pymongo
- Client = mongoclient (' mongodb://localhost:27017/')
Connecting to a database
- db = Client.mydb
Or
- db = client[' mydb ')
Connecting clustered aggregates equivalent to tables in relational databases
- 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 records query 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 ()
Sort the query Result sort column
- Collection.find (). Sort ("Key1") # Default 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)])
The Pymongo of MongoDB