Python Operation MongoDB Database

Source: Internet
Author: User

Online about Python operation MongoDB related articles are relatively not many, and the quality is not very high! The following gives a complete delete and change to check the sample program!

#!/usr/bin/python#-*-coding:utf-8-*-ImportPymongoImportreconnection= Pymongo. Mongoclient ('10.38.164.80', 27017) TDB=connection.testcollection=tdb.article#Inserting DataTry: Insert_data={"ID":"2","value":"ABC"} collection.insert (Insert_data)exceptbaseexception:Print "Insert Exception"#Querying DataTry:    PrintCollection.find_one ({"ID":"2"}) Cursor= Collection.find ({"title": Re.compile ("^. {0,50} (goddess)")},{"title": 1,"URL": 1})     forResultinchcursor:PrintType (Result)#View Type        PrintSTR (result). Decode ("Unicode-escape")        PrintResult.get ("title")        #Print str (result). Decode (' Unicode_escape ')exceptbaseexception,e:Print "Querying data Exceptions"+Str (e)#Modifying DataTry: Collection.update ({"ID":"2"},{"$set":{"value":"123"}})exceptbaseexception,e:Print "failed to update data"    Printe#Delete DataTry: Collection.remove ({"ID":"2"})exceptbaseexception,e:Print "Delete Data Exception"    PrintE

To install the MONGO library, install the following command (Ubuntu): Pip Install Pymongo

Python Operation MongoDB Database

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.