from pymongo import MongoClient
# 连接数据库
client = MongoClient("localhost", 27017)
# 获取数据库
db = client["Students"]
# 获取集合
students_collections = db["StudentsTable"]
# books_collections = db["books_info"]
# 查询students记录
# for i in students_collections.find():
# print(i)
# for i in students_collections.find({"age":{"$gte":5}}):
# print(i)
# 插入记录
# students = [
# {"姓名": "张三", "学号": "2020123456", "专业": "计算机科学", "年级": "大二", "联系方式": "zhangsan@example.com"},
# {"姓名": "李四", "学号": "2020123457", "专业": "电子工程", "年级": "大一", "联系方式": "lisi@example.com"},
# {"姓名": "王五", "学号": "2020123458", "专业": "软件工程", "年级": "大三", "联系方式": "wangwu@example.com"},
# {"姓名": "赵六", "学号": "2020123459", "专业": "信息管理", "年级": "大四", "联系方式": "zhaoliu@example.com"}]
# students_collections.insert_many(students)
for i in students_collections.find():
print(i)
# 删除记录
# res=students_collections.delete_one({"name":"liu","age":"18"})
# print(res.deleted_count,res)
# 修改记录
# res=books_collections.update_one({"author": "Mike"}, {"$set": {"text": "Updated text."}})
# print(res)
MongoDB学习笔记
最新推荐文章于 2025-06-06 16:46:12 发布