MongoDB essay, Pymongo simple to use

Source: Internet
Author: User

Installing MongoDB

MongoDB downloads the corresponding system version of the executable file
My system environment: rhel-server-6.2-x86_64
Unpack the Packagetar zxvf mongodb-linux-x86_64-rhel62-3.0.2.tgz

You can view the Readme in the directory to understand the role of each executable file.
Simple Start Command mkdir db; mongo --dbpath=./db
mongo --helpYou can get more help.

Installing Pymongo

installation command: pip install pymongo
For more Pip applications, refer to the Python pip pydoc 2to3 tool

Pymongo Simple to use
#!/usr/bin/env python#-*-Coding:utf-8-*-ImportPymongoImportDatetime def get_db():    # Establish a connectionClient = Pymongo. Mongoclient (host="10.244.25.180", port=27017) db = client[' Example ']#或者 db = Client.example    returnDb def get_collection(db):    # Select Collection (Mongo in collection and database are created by inertia)coll = db[' informations ']PrintDb.collection_names ()returnColl def insert_one_doc(db):    # Insert a documentcoll = db[' informations '] Information = {"Name":"Quyang","Age":"very"} information_id = Coll.insert (information)Printinformation_id def insert_multi_docs(db):    # Insert documents in bulk, insert an arraycoll = db[' informations '] Information = [{"Name":"Xiaoming","Age":"very"}, {"Name":"Xiaoqiang","Age":" the"}] information_id = Coll.insert (information)Printinformation_id def get_one_doc(db):    # There's just one return, no return nonecoll = db[' informations ']PrintColl.find_one ()# Returns the first record    PrintColl.find_one ({"Name":"Quyang"})PrintColl.find_one ({"Name":"None"}) def get_one_by_id(db):    # Find a doc with Objectidcoll = db[' informations '] obj = Coll.find_one () obj_id = obj["_id"]Print "_id for Objectid type, obj_id:"+ STR (obj_id)PrintColl.find_one ({"_id": obj_id})# Note that the obj_id here is an object, not a str, and cannot be found using the STR type as the _id value.    Print "_id is str type"    PrintColl.find_one ({"_id": Str (OBJ_ID)})# The Objectid method can be used to turn str into Objectid type     fromBson.objectidImportObjectIdPrint "_id converted to Objectid type"    PrintColl.find_one ({"_id": ObjectId (str (obj_id))}) def get_many_docs(db):    # MONGO provides a method for filtering lookups, which can be used to get data sets by various criteria filters, and to count, sort, and process data .coll = db[' informations ']#ASCENDING = 1 ascending; descending = 1 descending; default is ascending     forIteminchColl.find (). Sort ("Age", Pymongo. Descending):PrintItem count = Coll.count ()Print "All data in collection%s"% Int (count)#条件查询Count = Coll.find ({"Name":"Quyang"}). Count ()Print "Quyang:%s"%count def clear_all_datas(db):    #清空一个集合中的所有数据db["Informations"].remove ()if__name__ = =' __main__ ': db = get_db () my_collection = get_collection (db) post = {"Author":"Mike.","Text":"My First blog post!","tags": ["MongoDB","Python","Pymongo"],"Date": Datetime.datetime.utcnow ()}# Insert RecordMy_collection.insert (POST) Insert_one_doc (db)# Conditional Query    PrintMy_collection.find_one ({"x":"Ten"})# Query All the data in the table     forIiiinchMy_collection.find ():PrintIiiPrintMy_collection.count () my_collection.update ({"Author":"Mike."},                         {"Author":"Quyang","Text":"My First blog post!","tags": ["MongoDB","Python","Pymongo"],"Date": Datetime.datetime.utcnow ()}) forJjjinchMy_collection.find ():PrintJJJ Get_one_doc (db) get_one_by_id (db) Get_many_docs (db)# Clear_all_datas (db)

MongoDB essay, Pymongo simple to use

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.