更新單一文件
看起來資料庫裡關於 The Revenant 的紀錄有錯誤。
上映年份不正確,而且少了它的奧斯卡獎項。你可以在互動殼層中執行以下程式碼來看到這些錯誤:
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(____)