Common operations for Pymongo

Source: Internet
Author: User

Environment: Pymongo3.0.3,python3

Here are some of the things I have done about Pymongo, and many of them are using Pymongo.  Connecion () to connect to the database, but I am connected here has been prompted without this package, if you have any solution or other needs to add, also welcome to tell me.  
First, import Pymongo, connect to the database using Mongclient, connect to MyInfo database
Import Pymongo
Client= Pymongo. Mongoclient ("127.0.0.1", 27017)
Db=client.myinfo
Second,Insert, Insert_one () can only insert a single piece of data, insert multiple data format is Db.user.insert ([{Number of 1},{2}]), must add [], otherwise only added to the first (user is a collection, In addition to using db["collection", you can also use the db.collection to manipulate the collection
db["User"].insert_one ({"Name": "Zhao"})
db["User"].insert_one ({"Name": "Zhou", "Age": "5"})
db["User"].insert ([{"Name": "WU", "Age": "6"},{"name": "Zheng", "Age": "7"}])
*insert can also be inserted in this way to separate data
data = [
{"Name": "Zhao", "Rank": "1"},
        {"Name": "Qian", "Rank": "2"},
{"Name": "Sun", "Rank": "3"},
{"name": "Li", "Rank": "4"},
]
Db.user.insert (data)
Updates ,$set: Update operation, Multi=true: whether to operate on all the data queried, Upsert=true: If the results of the query are not found insert a piece of data

Db.user.update_one ({"Age": "2"},{"$set": {"name": "Qian", "Age": 2}})
Db.user.update ({"Name": "Sun"},{"$set": {"name": "Qian"}},upsert=true)
*update_one is also able to operate on only one piece of data, $set is the $ operator for the update operation, or it can be used with $inc or $push, with the first two operating speeds similar to $push slow operation.
Remove, if the following () does not fill in the content, is the entire table is emptied, db.user.find_one_and_delete () is also the meaning of deletion

Db.user.remove ({"Name": "WU"})
Db.user.find_one_and_delete ({"Name": "Zheng"})
Five, Db.user.count (), statistical query out of the number , () do not fill in the thing, is to count all the data in the collection

Print (Db.user.count ({"Age": "6"})
Six, print out the results of the query
From Bson import Json_util as Jsonb

Print (Jsonb.dumps (list (Db.user.find ({"Name": "WU"})))
Print (Db.user.find ({"Name": "WU"}))
You can see the above two ways, the non-conversion and converted results are compared as follows:

*jsonb.dumps () Converts the results of the query to a readable list format, otherwise the <pymongo.cursor.cursor object at 0x02096df0> is printed
Traverse the Col1=db.user.find () query to all the results, and its key=name value
For I in col1:
Print (i)
Print (i["name"])

Common operations for Pymongo

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.