開始使用免費開始

插入多筆文件

有時你需要一次新增多筆文件,而不是反覆呼叫 .insert_one()。MongoDB 的 .insert_many() 正是為此而設計。

本練習屬於課程

Python 中的 MongoDB 入門

檢視課程

練習說明

  • 完成 new_movies 的定義,分別將片名設為:"arrival""m3gan"
  • 將這些電影插入 film 資料庫中的 movies 集合。
  • 列印新插入文件的自動產生 id。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Complete definition of new_movies
new_movies = [
    {
      "title": ____,
      "genre": ["mystery", "sci-fi"],
      "release_year": 2016, "rating": 7.9
    },
    {
		"title": ____,
		"genre": ["horror", "sci-fi", "thriller"],
		"release_year": 2023, "rating": 6.4
  }
]

# Insert new movies
res = ____

# Print out the auto-generated ids
print(___)
編輯並執行程式碼