开始使用免费开始使用

更新多个文档

有时需要对多个文档进行相同的更新,例如纠正错误,或为满足特定条件的记录打标签。这时就可以使用 .update_many()

在本练习中,您将把那些获得过奥斯卡奖且评分高于 7.0 的电影标记为收藏。

本练习是课程的一部分

Python 中的 MongoDB 入门

查看课程

练习说明

  • 定义 query_filter,使其匹配所有 "won_oscar" 等于 True"rating" 大于 7.0 的电影。
  • 定义 update,使用 $set 将新字段 "is_favorite" 设为 True
  • 对所有匹配 query_filter 的文档执行更新。
  • 打印此次更新影响的文档数量。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Filter for Oscar winning movies with rating greater than 7.0
query_filter = {
  "____": ____,
  "____": { "____": ____ }
}

# Update to set is_favorite to True
update = ____

# Perform update for all documents that match filter
res = ____

# How many documents were affected?
print(____)
编辑并运行代码