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 environment. Suppose you have some knowledge about mongo and some command line operations.

Installation and update
Like installing most py packages, you can install them through 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 use of pymongo
'''

From pymongo import MongoClient

Def get_db ():
# Establish a connection
Client = strongclient ("localhost", 27017)
# Test, there are other writing methods
Db = client. test
Return db

Def get_collection (db ):
# Select a collection (collection and database in mongo are created by lazy. For details, refer to google)
Collection = db ['posts']
Print collection

Def insert_one_doc (db ):
# Insert a document
Posts = db. posts
Post = {"name": "lzz", "age": 25, "weight": "55 "}
Post_id = posts. insert (post)
Print post_id

Def 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: queries the entire set, root ObjectId queries, and query by a Field
Def get_all_colls (db ):
# Obtain the names of all sets in a database
Print db. collection_names ()

Def get_one_doc (db ):
# If yes, one is returned. If no, None is returned.
Posts = db. posts
Print posts. find_one ()
Print posts. find_one ({"name": "jack "})
Print posts. find_one ({"name": "None "})
Return

Def get_one_by_id (db ):
# Use objectid to find a doc
Posts = db. posts
Obj = posts. find_one ()
Obj_id = obj ["_ id"]
Print "_ id is of the ObjectId type :"
Print posts. find_one ({"_ id": obj_id })
# Note that the obj_id here is an object instead of a str. records cannot be found using the str type as the value of _ id.
Print "_ id: 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 converted to ObjectId type"
Print posts. find_one ({"_ id": ObjectId (str (obj_id ))})

Def get_many_docs (db ):
# Mongo provides a method for filtering searches.
# Conditional filtering is used to obtain a dataset, and data can be counted, sorted, and processed.
Posts = db. posts
# All data, sorted by age,-1 is inverted
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 I

Def clear_coll_datas (db ):
# Clear all data in a set
Db. 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.

MongoDB Python driver PyMongo

Install the MongoDB Development Environment PyMongo

This article permanently updates the link address:

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.