Python operation MongoDB (use of Pymongo module)

Source: Internet
Author: User

Study notes, used as database queries, original reference

1 #!/usr/bin/env python2 #Coding:utf-83 #Author:--<qingfengkuyu>4 #Use of Purpose:mongodb5 #CREATED:2014/4/146 #32-bit versions can store up to 2.5GB of data (nosqlfan: Maximum file size 2G, production environment 64-bit recommended)7  8 ImportPymongo9 ImportdatetimeTen ImportRandom One   A #Create a connection -conn = Pymongo. Connection ('10.11.1.70', 27017)#now use Pymongo. Mongoclient Connection - #connecting to a database thedb =Conn.study - #db = conn[' study ') -   - #Print all clustered names, connect to aggregation + PrintU'All aggregates:', Db.collection_names () -Posts =Db.post + #posts = db[' post '] A Printposts at   - #Inserting Records -New_post = {"AccountID": 22,"UserName":"libing",'Date':d Atetime.datetime.now ()} -New_posts = [{"AccountID": 22,"UserName":"Liuw",'Date':d Atetime.datetime.now ()}, -{"AccountID": 23,"UserName":"urling",'Date':d Atetime.datetime.now ()}]#each record has a different insertion time -   in Posts.insert (new_post) - #Posts.insert (new_posts) #批量插入多条数据 to   + #Deleting Records - PrintU'Delete the specified record: \ n', Posts.find_one ({"AccountID": 22,"UserName":"libing"}) thePosts.remove ({"AccountID": 22,"UserName":"libing"}) *   $ #modify records within a clusterPanax NotoginsengPosts.update ({"UserName":"urling"},{"$set":{'AccountID': Random.randint (20,50)}}) -   the #query record, statistics record quantity + PrintU'The total record is:', Posts.count (), Posts.find (). Count () A PrintU'query a single record: \ n', Posts.find_one () the PrintPosts.find_one ({"UserName":"Liuw"}) +   - #querying all Records $ PrintU'querying multiple records:' $ #For item in Posts.find (): #查询全部记录 - #For item in Posts.find ({"UserName": "Urling"}): #查询指定记录 - #for item in Posts.find (). Sort ("UserName"): #查询结果根据UserName排序, ascending by default the #for item in Posts.find (). Sort ("UserName", Pymongo. Ascending): #查询结果根据UserName排序, Ascending is ascending, descending is descending -  forIteminchPosts.find (). sort ([("UserName", Pymongo. Ascending), ('Date', Pymongo. Descending)]):#query results sorted by multiple columnsWuyi     PrintItem the   - #to view the performance of a query statement Wu #Posts.create_index (["UserName", Pymongo. Ascending), ("Date", Pymongo. Descending)]) #加索引 - PrintPosts.find (). sort ([("UserName", Pymongo. Ascending), ('Date', Pymongo. Descending)]. Explain () ["cursor"]#querying records with Basiccursor without index About PrintPosts.find (). sort ([("UserName", Pymongo. Ascending), ('Date', Pymongo. Descending)]. Explain () ["nscanned"]#the number of records queried when the query statement was executed

Python operation MongoDB (use of Pymongo module)

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.