Pymongo Usage Summary

Source: Internet
Author: User
Tags mongoclient

0. What is Pymongo

Pymongo is the Python module that operates MongoDB

1, installation Pymongo
# Easy_install Pymongo
2. Connect MongoDB
>>> Import Pymongo>>> conn = Pymongo. Mongoclient (Host=host,port=port,tz_aware=false)
3. Get a list of databases
>>> conn.database_names () [u'test1', u'test2' , u'admin', u'local']
4. Connect to the database
>>> db = Conn.get_database ("db_name")
5. Permission Verification
>>> db.authenticate ('username'password')
True
6. Get the aggregation list (the concept of aggregation is similar to a table in a relational database)
>>> db.collection_names () [u' account', u'role' , u 'item', u'online')
7. Connection Aggregation
>>> account = db.get_collection ("col_name")
8. View a record of the aggregation
>>> Account.find_one ()
9. View all the keys that are clustered (similar to the fields in the relational database)
>>> Account.find_one (). Keys ()
10. View all collected Records
 for  in account. Find (): .... Print I
11. View the total number of records
>>> account. Find (). Count ()
12. Query multiple records according to the conditions
 for  in account. Find ({"name""xxx"}): .... Print I
13. Sort the query results (default ascending ascending)
>>> account. Find (). Sort ("name", Pymongo. Ascending)>>> account. Find (). Sort (["name", Pymongo. Ascending), ("active_time", Pymongo. Descending)])
14. New Record
>>> Account.insert ({"name""Mike" " Active_time""20130408"})
15. Update record
>>> Account.update ({"name":"Mike"}, {"$set": {"Active_time":"20130408120000"}}) Note: If there are no keys in the data-Value"name":"Mike", you will add"Active_time":"20130408120000"
16. Delete record (without condition means delete all)
>>> Account.remove ({"name""Mike"})
Pycharm Ambiguous matching query
method 1.import re{'xxx': Re.compile ('xxx')}
Method 2. {'xxx': {'$regex':'xxx '}
18.and or
Account. Find ' Test ' ' Test '} } ] })
19. Case studies
#! /usr/bin/Envpython#--*--coding:utf-8--*--Import Pymongoimport rehost="**.**.**.**"Port=27017Conn= Pymongo. Mongoclient (host=host,port=port,tz_aware=False) db_name="Test"Log_name="Col"db_test=conn.get_database (db_name) Col_col=db_test.get_collection (log_name) #print"db names:", Conn.database_names () #print"col Names:", Db_test.collection_names () #print"One_line:", Col_col.find_one () #print"One_line_keys:", Col_col.find_one (). Keys () #col_col. Insert ({"Message2":"Hihihi","msg":"123"}) #col_col. Update ({"message":"hello*3"}, {"$set":{"message":"hello*3*9"}}) import Datetimey,m,d,h,m,s= .,2, -, -, -,0D=datetime.datetime (y,m,d,h,m,s) Delta= Datetime.timedelta (hours=8) d= dDelta#logs= Col_col.Find( {"Data":{"$GT":d}) #logs= Col_col.Find( {"message": Re.compile ("Hi")}) logs= Col_col.Find( {"$and": [{"message": Re.compile ("Hi")}, {"Data":{"$GT":d}}]}) C=Logs.count () forLinchLogs:print Lprint"Count:", Cprint"end!"

Pymongo Usage Summary

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.