进入虚拟环境
sudo pip install pymongo
或源码安装
python setup.py
import pymongo
类MongoClient
无安全认证:client=pymongo.MongoClient(
‘mongodb://localhost:27017‘)
有安全认证:client=pymongo.MongoClient(
‘mongodb://用户名:密码@localhost:27017/数据库名称‘)
类database
db=client.test1
类collection
stu = db.stu
s1={name:‘gj‘,age:18}
s1_id = stu.insert_one(s1).inserted_id
print(s1_id)
scores.update_one({‘name‘:‘zsf‘},{‘$set‘:{‘name‘:‘张三丰‘}})
scores.delete_one({‘name‘:‘zsf‘})
ret = stu.find_one()
print(ret)
print(ret[‘name‘])
ret = stu.find_one({‘name‘:‘张三丰‘})
print(ret)
print(ret[‘name‘])
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‘])
cur=stu.find()
cur.next()
cur.next()
cur.next()
print stu.count()
单属性:cur = stu.find().sort(‘age‘, DESCENDING)
多属性:cur = stu.find().sort([(‘age‘, DESCENDING),(‘name‘, ASCENDING)])
cur=stu.find().skip(2).limit(3)
原文:https://www.cnblogs.com/LiuYanYGZ/p/12241900.html