在 MongoDB 中執行批次插入?


要批次插入 MongoDB,請使用 initializeUnorderedBulkOp()。讓我們建立一個包含文件的集合 -

> var bulkInsertDoc = db.demo663.initializeUnorderedBulkOp();
> bulkInsertDoc.insert( { Name: "John",CountryName:"US"} );
> bulkInsertDoc.insert( { Name: "Chris",CountryName:"UK"} );
> bulkInsertDoc.insert( { Name: "David",CountryName:"AUS"} );
> bulkInsertDoc.execute();
BulkWriteResult({
   "writeErrors" : [ ],
   "writeConcernErrors" : [ ],
   "nInserted" : 3,
   "nUpserted" : 0,
   "nMatched" : 0,
   "nModified" : 0,
   "nRemoved" : 0,
   "upserted" : [ ]
})

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

> db.demo663.find();

這將產生以下輸出 -

{ "_id" : ObjectId("5ea1b41424113ea5458c7d05"), "Name" : "John", "CountryName" : "US" }
{ "_id" : ObjectId("5ea1b41424113ea5458c7d06"), "Name" : "Chris", "CountryName" : "UK" }
{ "_id" : ObjectId("5ea1b41424113ea5458c7d07"), "Name" : "David", "CountryName" : "AUS" }

更新於: 2020 年 5 月 13 日

377 次觀看

開啟您的 職業生涯

完成課程以獲得認證

開始
廣告
© . All rights reserved.