编程要求
根据提示,在右侧编辑器 Begin-End 处补充代码,完成右侧程序。
测试说明
点击评测,平台会对你编写的代码进行测试。
import pymongo
MonClient = pymongo.MongoClient("mongodb://localhost:27017/")
MonDb = MonClient["MonDbTest"]
MonCol = MonDb["ColTest"]
def mongo_Insert():
docl = {"_id": 1, "city": "武汉", "province": "江苏", "code": "213001"}
flag = MonCol.find_one({"_id": 1})
if flag is None:
MonCol.insert_one(docl)
else:
MonCol.delete_one({"_id": 1})
MonCol.insert_one(docl)
def mongo_Query():
res = MonCol.find()
for i in res:
print(i)
def mongo_Update():
# 请将{"_id": 1, "city": "武汉", "province": "江苏", "code": "213001"},
# 修改为{"_id": 1, "city": "武汉", "province": "湖北", "code": "430000"},
########### Begin ###########
MonClient = pymongo.MongoClient("mongodb://localhost:27017/")
MonDb = MonClient["MonDbTest"]
MonCol = MonDb["ColTest"]
queryKey = {"province": "江苏"}
updateKey = {"$set": {"province": "湖北", "code": "430000"}}
res = MonCol.update_one(queryKey, updateKey, True)
########### End ###########
if __name__== "__main__":
mongo_Insert()
print("修改前:")
mongo_Query()
mongo_Update()
print("修改后:")
mongo_Query()