进入虚拟环境
sudo pip install pymongo
导入包pymongo
import pymongo
连接,创建客户端
client = pymongo.MongoClient("localhost",27017)
获取数据库test
db = client.test
获取集合student
stu = db.student
添加文档
m1 = {name:'xiaoming',age:18}
m1_id = stu.insert_one(m1).inserted_id
查找一个文档
m2 = stu.find_one()
查找多个文档1
for txt in stu.find():
print(txt)
查找多个文档2
txt = stu.find()
txt.next()
txt.next()
txt.next()
获取文档个数
print(stu.count())
网友评论