插入單一文件
在多次練習讀取與篩選資料之後,現在換個角度,開始把文件加入你的 MongoDB 集合吧。
⚠️ 注意!本章的練習會修改資料庫。若你多次執行相同的程式碼,結果可能會不同。想要回到乾淨狀態,請重新整理頁面。
本練習屬於課程
Python 中的 MongoDB 入門
練習說明
- 完成字典
new_movie,讓它包含類型"drama"與"mystery"。 - 將
new_movie插入 film 資料庫中的 movies 集合。 - 列印新插入文件的自動產生 id。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Complete the definition of new_movie
new_movie = {
"title": "the prestige",
"genre": ____,
"release_year": 2006,
"rating": 8.5,
"won_oscar": False
}
# Insert new_movie into movies collection
res = ____
# Print its auto-generated id
print(res.____)