1.安装python包
进入虚拟环境 sudo pip install pymongo 或源码安装 python setup.py
2.引入包pymongo
import pymongo
3.连接,创建客户端
client=pymongo.MongoClient("localhost", 27017)
4.获得数据库test1
db=client.test1
5.获得集合stu
stu = db.stu
6.添加文档
s1={name:'gj',age:18} s1_id = stu.insert_one(s1).inserted_id
7.查找一个文档
s2=stu.find_one()
8.查找多个文档1
for cur in stu.find(): print cur
9.查找多个文档2
cur=stu.find() cur.next() cur.next() cur.next()
10.获取文档个数
print stu.count()