Pymongo installation using Notes _python

Source: Internet
Author: User
Tags mongoclient

Here is a simple installation and use of records, first of all to have an available MONGO environment, win or Linux environment can be. Suppose you have some knowledge of MONGO and know some command-line operations.

Install and update
Like most PY pack installs, it can be installed in the source code, or it can be installed using PIP or Easy_install.

Installation

Copy Code code as follows:

Pip Install Pymongo

Upgrade
Copy Code code as follows:

Pip Install--upgrade Pymongo

Other installation methods Please refer to the document Pymongo installation

Operation

Website Tutorials

Small case

Copy Code code as follows:

#-*-Coding:utf-8-*-
#python2.7x
#author: Orangleliu @2014-09-24
'''
Simple use of Pymongo
'''

From Pymongo import mongoclient

Def get_db ():
#建立连接
Client = mongoclient ("localhost", 27017)
#test, there are other formulations.
db = Client.test
Return db

def get_collection (db):
#选择集合 (MONGO collection and database are lazy created, can be specific google)
Collection = Db[' posts ']
Print Collection

def insert_one_doc (db):
#插入一个document
Posts = db.posts
Post = {"name": "Lzz", "Age":, "Weight": "55"}
post_id = Posts.insert (POST)
Print post_id

def insert_mulit_docs (db):
#批量插入documents, insert an array
Posts = db.posts
Post = [{"Name": "Nine", "age":], "weight": "55"},
{"Name": "Jack", "Age": "Weight": "55"}]
Obj_ids = Posts.insert (POST)
Print Obj_ids

# #查询, you can query the entire collection, you can root Objectid query, can be based on a field query, etc.
def get_all_colls (db):
#获得一个数据库中的所有集合名称
Print Db.collection_names ()

def get_one_doc (db):
#有就返回一个, none returned none
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):
#通过objectid来查找一个doc
Posts = db.posts
obj = Posts.find_one ()
obj_id = obj["_id"]
Print "_id is Objectid type:"
Print Posts.find_one ({"_id": obj_id})
#需要注意这里的obj_id是一个对象, not a str, a record cannot be found using the str type as a _id value
print "_id for str type"
Print Posts.find_one ({"_id": Str (OBJ_ID)})

#可以通过ObjectId方法把str转成ObjectId类型
From Bson.objectid import Objectid
Print "_id convert to Objectid type"
Print Posts.find_one ({"_id": ObjectId (str (obj_id))})

def get_many_docs (db):
#mongo中提供了过滤查找的方法, you can pass each
#种条件筛选来获取数据集, you can also count, sort, and handle data.
Posts = db.posts
#所有数据, sorted by age,-1 is in reverse order.
all = Posts.find (). Sort (' age ',-1)

Count = Posts.count ()
All data in the print ' collection%s '%int (count)
For I in all:
Print I

#条件查询
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):
#清空一个集合中的所有数据
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 all about writing simple operations, as for set operations, group operations, etc. later in the summary.

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.