1、启动mongodb服务(请自行查询)
2、cmd命令行安装三方库:
pip install pymongo
import pymongo
from bson.objectid import ObjectId #使用id查询时的库
#连接服务器
conn = pymongo.MongoClient("localhost",27017)
#连接数据库
db = conn.mydb
#获取集合
collection = db.student
'''
1、添加文档
#collection.insert({"name":"朱自清","age":15,"gender":1,"isDelete":0})
'''
'''
#2、查询文档
res = collection.find({"age":24})
for row in res:
print(row)
print(type(row))
'''
'''
#3、根据id查询
res = collection.find({"_id":ObjectId("5c2b82e1464bc020a8d2a414")})
print(res[0])
'''
'''
#4、排序
res = collection.find().sort("age") #升序
res2 = collection.find().sort("age",pymongo.DESCENDING) #升序
for row in res2:
print(row)
print(type(row))
'''
'''
5、分页查询
res = collection.find().skip(3).limit(5)
'''
'''
6、更新
collection.update({"name":"朱自清"},{"$set":{"age":60}})
7、删除
collection.remove({"name":"朱自清"})
'''
collection.remove({"name":"朱自清"})
conn.close()