Python Study Notes _ 03: simple operations on MongoDB database, _ 03 mongodb

Source: Internet
Author: User
Tags mongoclient

Python Study Notes _ 03: simple operations on MongoDB database, _ 03 mongodb

Directory

1. Insert document

2. query documents

3. Update the document

4. delete a document

 1. Insert document
# -*- coding: UTF-8 -*-import datetimefrom pymongo import MongoClientclient = MongoClient()db = client.bookuser = {"name": "daming12",         "pwd": "asdf33",         "tags": ["mongodb11", "python11", "pymongo11"],         "date": datetime.datetime.utcnow()}users = db.useruser_id = users.insert_one(user).inserted_idprint ("user id is ", user_id)

Open MongoBooster to view all documents in the user collection in the book database:

After the insert operation is executed, the console outputs the following information:

 

 

2. Query documents

Query all the sets contained in the current database:

# -*- coding: UTF-8 -*-import datetimefrom pymongo import MongoClientclient = MongoClient()db = client.bookcur_collection = db.collection_names(include_system_collections=False)print("cur_collection is :", cur_collection)

Console output information:

 

Query the specific document information in the database collection:

#-*-Coding: UTF-8-*-import datetimefrom pymongo import Export clientclient = export client () db = client. bookusers = db. user # query all for I in users. find (): print (I) # query for I in users of name = liuzhen. find ({"name": "liuzhen"}): print (I) # query the print (users. find_one ({"name": "daming "}))

 

Console output information:

 

 

 

3. Update document

Update the operation syntax template:

Collection. update (<query>, # query condition <update>, # update object and some updated operators {upsert: <boolean>, # whether to insert multi if no update record exists: <boolean>, # optional. The default value of mongodb is false. Only the first record writeConcern found is updated: <document> # optional. The exception level is thrown. })

Update example:

# -*- coding: UTF-8 -*-import datetimefrom pymongo import MongoClientclient = MongoClient()db = client.bookusers = db.userprint users.find_one({"name":"liuzhen"})users.update({"name":"liuzhen"},{'$set':{"pwd":"98765432d1"}})print users.find_one({"name":"liuzhen"})

Console output information:

 

 

4. Delete document

Delete operation statement template:

Collection. remove (<query>, # (optional) Conditions for deleting a document {justOne: <boolean>, # (optional) If set to true or 1, only one document writeConcern is deleted: <document> # (optional) level of exception thrown })

 

Specific deletion operation example:

#-*-Coding: UTF-8-*-import datetimefrom pymongo import Export clientclient = export client () db = client. bookusers = db. userprint "All document information in the collection user before deletion:" # query all for I in users. find (): print (I) # delete all users whose name = lisi records. remove ({'name': 'daming'}) # Delete the record id = users. find_one ({"name": "liuzhen"}) ["_ id"] users. remove (id) print "after deletion, all document information in the collection user:" # query all for I in users. find (): print (I) # delete all records in the set # db. users. remove ()

Console output information:

 

 

References:

1. Connect Python to MongoDB

2. python operations on MongoDB

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.