更新多筆文件
有時候需要對多筆文件做相同的更新,例如更正錯誤,或是為符合特定條件的紀錄加上標記。這時就會用到 .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(____)