python与mongdb数据库的连接
from pymongo import MongoClient
#1.使用客户端连接服务器
conn=MongoClient("127.0.0.1",27017)
#2.选择要操作的数据库
db=conn.mydb1
#3.选择数据库中要操作的集合
collection=db.student
#4.添加文档
#collection.insert({"name":'军军',"age":18,"sex":"male"})
collection.insert([{"name":'军军',"age":18,"sex":"male"},
{"name": '彬彬', "age": 20, "sex": "male"},
{"name": '图图', "age": 25, "sex": "male"},
{"name": '乐乐', "age": 24, "sex": "male"}])
'''
#4.更新文档 True 代表是否修改多条记录
collection.update({"name":'军军'},{"$set":{"age":30,"sex":"女"}},True)
#4.删除文档
collection.remove({"name":"军军"})
#4.查询文档
# reslist=collection.find()
# for res in reslist:
# print(res)
#根据条件查询 年龄大于20 的
# reslist=collection.find({"age":{"$gt":20}})
# for res in reslist:
# print(res)
#查询年龄大于20 的人的个数
cou = collection.find({"age":{"$gt":20}}).count()
print(cou)
#按年龄进行排序 降序 pymongo.DESCENDING
# reslist=collection.find().sort("age",pymongo.DESCENDING)
# for res in reslist:
# print(res)
#分页 limit 跳过 skip
# reslist=collection.find().skip(1).limit(1)
# for res in reslist:
# print(res)
'''
#5.断开连接
conn.close()