2.5, python interact with
- Click to view the official documentation
- Install python package
进入虚拟环境
sudo pip install pymongo
或源码安装
python setup.py
- The introduction of package pymongo
import pymongo
类MongoClient
- Connection, create a client
无安全认证:client=pymongo.MongoClient(
'mongodb://localhost:27017')
有安全认证:client=pymongo.MongoClient(
'Mongodb: // username: password @localhost: 27017 / database name')
类database
- Get the database test1
db=client.test1
类collection
- The main methods are as follows
- insert_one()
- insert_many()
- update_one()
- update_many()
- delete_one()
- delete_many()
- find_one()
- find()
- Gets a collection of stu
stu = db.stu
- Add a document, you can return the document id
s1={name:'gj',age:18}
s1_id = stu.insert_one(s1).inserted_id
print(s1_id)
- Modify the document
scores.update_one({'name':'zsf'},{'$set':{'name':'张三丰'}})
- Delete Document
scores.delete_one({'name':'zsf'})
- Find a document, convert the document to return a dictionary
K = stu.find_one ()
print (right)
print (right [ 'Name'])
K = stu.find_one ({ 'name', '张三丰'})
the printer (right)
print (right [ 'Name'])
- 1 when looking for multiple documents, returns an object of type Cursor, used to traverse, traverse, each document returned as a dictionary
cursor = stu.find()
for s in cursor:
print(s)
print(s['name'])
cursor = stu.find({'name':'张三丰'})
for s in cursor:
print(s)
print(s['name'])
- Find more documents 2
cur=stu.find()
cur.next()
cur.next()
cur.next()
- Gets the number of documents
print stu.count()
- Sort, returns an object of type cursor
- Used in ascending order ASCENDING, descending use DESCENDING:
Single property: cur = stu.find () sort ( 'age', DESCENDING).
Multiple Attribute: cur = stu.find () sort ([( 'age', DESCENDING), ( 'name', ASCENDING)]).
- Child Collection
cur=stu.find().skip(2).limit(3)
# -*- coding:utf-8 -*- # @Time : 2020/1/29 23:16 # @Author : xxxx # @File : test1_mongodb.py # @Software: PyCharm import pymongo DEF test0 (): # get the client to establish a connection CLI = pymongo.MongoClient ( ' MongoDB: // localhost: 27017 ' ) # switching database DB = cli.liuyan # Get set Scores = db.scores # Add scores.insert_one ({ ' name ' : ' Zhang Yifeng ' }) # Delete scores.delete_one ({ ' name ' : ' Zhang Yifeng ' }) # Modify scores.update_one ({ ' name ' : ' ZSF ' }, { ' $ SET ' : { ' name ' : ' Chi Master ' }}) DEF test1 (): # get the client to establish a connection CLI = pymongo.MongoClient ( ' MongoDB: // localhost: 27017 ' ) # switching database DB = cli.liuyan # Get set Scores = db.scores # 查询 K = scores.find_one () print (right) the printer (right [ ' Name ' ]) DEF test2 (): # get the client to establish a connection CLI = pymongo.MongoClient ( ' MongoDB: // localhost: 27017 ' ) # switching database DB = cli.liuyan # Get set Scores = db.scores # Query RET = scores.find_one ({ ' name ' : ' Chi Master ' }) Print (RET) Print (RET [ ' name ' ]) DEF Test3 (): # get the client to establish a connection CLI = pymongo.MongoClient ( ' MongoDB: // localhost: 27017 ' ) # switching database DB = cli.liuyan # Get set Scores = db.scores # 查询 cursor = scores.find() for s in cursor: print(s) print(s['name']) DEF Test4 (): # get the client to establish a connection CLI = pymongo.MongoClient ( ' MongoDB: // localhost: 27017 ' ) # switching database DB = cli.liuyan # Get set Scores = db.scores # Query Cursor = scores.find ({ ' name ' : ' Chi Master ' }) for S in Cursor: Print (S) Print (S [ ' name ' ]) DEF Test5 (): # get the client to establish a connection CLI = pymongo.MongoClient ( ' MongoDB: // localhost: 27017 ' ) # switching database DB = cli.liuyan # Get set Scores = db.scores # 查询 # ret = scores.find({'age':{'$gt':20}}) # for s in ret: # print(s['name']) DEF test6 (): # get the client to establish a connection CLI = pymongo.MongoClient ( ' MongoDB: // localhost: 27017 ' ) # switching database DB = cli.liuyan # Get set Scores = db.scores # 查询、排序 # cursor = scores.find().sort('name', pymongo.DESCENDING) cursor = scores.find().sort([('name', pymongo.DESCENDING), ('age', pymongo.ASCENDING)]) for s in cursor: # print(s) print(s['name']) if __name__ == '__main__': # test0() # test1() # test2() # test3() # test4() # test5() test6()