為一次插入多個內容編寫 MongoDB 插入語句


對於多重插入,在 MongoDB 中使用 insert()。讓我們建立一個具有文件的集合 -

> db.demo689.insert([
...    {ClientName:"Chris","ClientAge":34,"ClientCountryName":"US"},
...    {ClientName:"David","ClientAge":28,"ClientCountryName":"UK"},
...    {ClientName:"Bob","ClientAge":39,"ClientCountryName":"AUS"},
... ]);
BulkWriteResult({
   "writeErrors" : [ ],
   "writeConcernErrors" : [ ],
   "nInserted" : 3,
   "nUpserted" : 0,
   "nMatched" : 0,
   "nModified" : 0,
   "nRemoved" : 0,
   "upserted" : [ ]
})

使用 find() 方法顯示集合中的所有文件 -

> db.demo689.find();

這將產生以下輸出 -

{ "_id" : ObjectId("5ea580dfa7e81adc6a0b3967"), "ClientName" : "Chris", "ClientAge" : 34, "ClientCountryName" : "US" }
{ "_id" : ObjectId("5ea580dfa7e81adc6a0b3968"), "ClientName" : "David", "ClientAge" : 28, "ClientCountryName" : "UK" }
{ "_id" : ObjectId("5ea580dfa7e81adc6a0b3969"), "ClientName" : "Bob", "ClientAge" : 39, "ClientCountryName" : "AUS" }

更新於: 14-5-2020

184 瀏覽

啟動您的職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.