Pymongo Library Simple Application (Python's MongoDB library) __python

Source: Internet
Author: User
Tags connect mongo mongoclient mongodb
Simple application of Pymongo library

The Pymongo library is a library of Python operations MongoDB. The following is a brief introduction to the simple use of Pymongo. Create MONGO client Select database and set merge insert Data

Import Pymongo

client = Pymongo. Mongoclient (host,port)
#client = Pymongo. Mongoclient (' mongodb://localhost:27017/')

db = Client.db #选择名称叫db的数据库, no
#或者db = client[' db '

will be created Collection = Db.test #选择test名的collection, no
#或者collection = db[' Test ' is created

#collection can manipulate
the collection Collection.insert ({' name ': ' AAA '}} #返回插入id
Collection.insert ([{' Name ': ' aaa '},{' name ': ' BBB '}]) #返回插入id的list
e = collection.insert_one ({' Name ': ' CCC '}) #返回结果通过e. inserted_id View Insert id

obj =  {' A ': ' B '}
Collection.insert_one (obj)
#插入成功后obj会多一个属性_id
print (obj) #{' _id ': ObjectId (' 58cf7eaac43b3761209924be ') , ' A ': ' B '
#insert也会产生相同的效果 to add an additional _id attribute to the inserted element to represent the ID after inserting the database
connection with certified MONGO

The above connection can connect MONGO that do not set a password, but MONGO with a password requires a different way of connecting.

#SCRAM-sha-1 way
uri = "Mongodb://user:password@example.com/the_database?authmechanism=scram-sha-1"
Client = Mongoclient (URI)

#或者
client = mongoclient (' example.com ')
client.the_database.authenticate (' User ', ' Password ', mechanism= ' scram-sha-1 ')

#MONGODB-cr Way from
Pymongo import mongoclient
uri = "Mongodb://user: PASSWORD@EXAMPLE.COM/THE_DATABASE?AUTHMECHANISM=MONGODB-CR "
client = mongoclient (URI)

#或者
client = Mongoclient (' example.com ')
client.the_database.authenticate (' user ', ' password ', mechanism= ' MONGODB-CR ')

#或者使用默认加密方式
uri = "Mongodb://user:password@example.com/the_database"
client = mongoclient (URI)
Querying Data
A = Collection.find ({' name ': ' AAA '}) #返回游标, you can get
a = Collection.find_one ({' name ': ' AAA '}) for an iteration or A.next () #返回结果
' {'
_id ': ObjectId (' 58b92cdfc43b3735664597be '), ' name ': ' AAA '} '
#如果id是字符串类型的 from
Bson.objectid import Objectid #导入ObjectId把字符串id转换成ObjectId才能查找
collection.find_one ({' _id ': Objectid (' 58b93904c43b3735664597c2 ')})
#直接使用字符串id是无法查询到内容的

#查看数据条数
collection.count ()
#或者 Collection.find (). Count ()
collection.find ({"Name": "AAA"}). Count ()

#条件查询和排序
d = datetime.datetime ( 2009, one,
collection.find ({"date": {"$lt": D}}). Sort ("author")
#限制查询条数和指定位置开始查询
Collection.find ({"date": {"$lt": D}}). Skip (1) #跳过符合条件的前一条, back to
collection.find (...). Limit (5) #查询5条结果
collection.find (...). Skip (5). Limit (5) #跳过前5条返回后面的5条
#排序
collection.find (...). Sort (' username ', Pymongo. Descending) #使用username排序DESCENDING为倒序, ascending positive sequence, (ascending=1,descending=-1)
#多条件排序
Collection.find (...). Sort (' username ', Pymongo. Descending), (' id ', Pymongo. Ascending))
Update
Import Pymongo
client = Pymongo. Mongclient (' 127.0.0.1 ')
db = client.test
collection = Db.user
collection.update ({' A ': ' B '}, {' $set ': {' a ') : ' B '}}; #update (condition, {' $set ': Update value})
Remove
result = Collection.delete_many ({' A ': ' B '}) #delete_many (condition)
result.delete_count  #删除个数 Result
= Collection.delete_one ({' A ': ' B '}) #delete_one (condition)
result.delete_count  #删除个数
Create an index

Create an index to speed up queries

Collection.create_index ([' user_id ', Pymongo. Ascending),], unique=true
#创建一个唯一的不可重复的索引user_id, multiple indexes can be created at a time

Refer to official documentation
Http://api.mongodb.com/python/current/tutorial.html

Note:
Use ORM Access MONGO use Mongoengine package
Document Http://docs.mongoengine.org

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.