更新单个文档
看起来数据库中关于 The Revenant 的条目有错误。
上映年份不正确,而且缺少是否获奥斯卡的字段。您可以在 shell 中运行以下代码查看这些问题:
mov.find_one({ "title": "the revenant" })
我们来修复它吧!
⚠️ 注意!本章的练习会修改数据库。如果您多次运行相同代码,结果可能会不同。若想从干净状态开始,请在浏览器中刷新页面。
本练习是课程的一部分
Python 中的 MongoDB 入门
练习说明
- 定义
query_filter,匹配电影"the revenant"(请使用该大小写)。 - 完成
update的定义,将"won_oscar"设为True,"release_year"设为2015。 - 使用
query_filter和update执行实际更新。 - 打印被修改的文档数量。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Define the title filter
query_filter = {
____: ____
}
# Define the update dictionary
update = {
____: {
____: ____,
____: ____
}
}
# Perform the update
res = mov.____
# How many records were modified?
print(____)