[Mongo] install and use PyMongo

Source: Internet
Author: User
Install and use PyMongo. Here is a simple installation and usage record. First, you must have an available mongo environment, either win or linux. Suppose you have some knowledge about mongo and some command line operations. Installation and update are the same as those of most py packages. you can install pip by using the source code or by using pip or easy_install.

Install and use PyMongo. Here is a simple installation and usage record. First, you must have an available mongo environment, either win or linux. Suppose you have some knowledge about mongo and some command line operations. Installation and update are the same as those of most py packages. you can install pip by using the source code or by using pip or easy_install.

Install and use PyMongo. Here is a simple installation and usage record. First, you must have an available mongo environment, either win or linux. Suppose you have some knowledge about mongo and some command line operations. Installation and update are the same as those for most py packages. you can install them by using the source code or pip or easy_install.

Install

Pip install pymongo

Upgrade

Pip install -- upgrade pymongo

For other installation methods, see pymongo.

Operation

Official website tutorial

Small Cases
#-*-Coding: UTF-8-*-# python2.7x # author: orangleliu @ 2014-09-24 ''' simple usage of pymongo ''' from pymongo import using clientdef get_db (): # establish a connection client = clients client ("localhost", 27017) # test, and write other statements db = client. test return dbdef get_collection (db): # select a collection (collection and database in mongo are created by lazy. For details, refer to google) collection = db ['posts'] print collectiondef insert_one_doc (db): # insert a document posts = db. posts post = {"name": "lzz", "age": 25, "weight": "55"} post_id = posts. insert (post) print post_iddef insert_mulit_docs (db): # insert documents in batches and insert an array posts = db. posts post = [{"name": "nine", "age": 28, "weight": "55" },{ "name": "jack ", "age": 25, "weight": "55"}] obj_ids = posts. insert (post) print obj_ids # query, which can be used to query the entire set, root ObjectId query, def get_all_colls (db) such as a field query ): # obtain print db for all set names in a database. collection_names () def get_one_doc (db): # If yes, one is returned. If no, None posts = db is returned. posts print posts. find_one () print posts. find_one ({"name": "jack"}) print posts. find_one ({"name": "None"}) returndef get_one_by_id (db): # Use objectid to find a doc posts = db. posts obj = posts. find_one () obj_id = obj ["_ id"] print "_ id: ObjectId type:" print posts. find_one ({"_ id": obj_id}) # note that the obj_id here is an object rather than a str, the record print "_ id is str type" print posts. find_one ({"_ id": str (obj_id)}) # You can use the ObjectId method to convert str to the ObjectId type from bson. objectid import ObjectId print "_ id to ObjectId type" print posts. find_one ({"_ id": ObjectId (str (obj_id)}) def get_many_docs (db): # mongo provides a method for filtering and searching, you can obtain the dataset by filtering the # conditions, and process the data such as Count and sort. posts = db. posts # all data, sorted by age,-1 is descending order all = posts. find (). sort ("age",-1) count = posts. count () print "% s of all data in the Set" % int (count) for I in all: print I # conditional Query count = posts. find ({"name": "lzz "}). count () print "lzz: % s" % count for I in posts. find ({"name": "lzz", "age": {"$ lt": 20}): print idef clear_coll_datas (db ): # Clear all data databases in a set. posts. remove ({}) if _ name _ = "_ main _": db = get_db () obj_id = insert_one_doc (db) obj_ids = insert_mulit_docs (db) # get_all_colls (db) # get_one_doc (db) # get_one_by_id (db) # get_many_docs (db) clear_coll_datas (db)

This is a simple write operation. As for Set Operations and group operations, we will summarize them later.

This article is from the "orangleliu notebook" blog, please be sure to keep this http://blog.csdn.net/orangleliu/article/details/39545751

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.