在 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.