官术网_书友最值得收藏!

  • Mastering MongoDB 3.x
  • Alex Giamas
  • 267字
  • 2021-08-20 10:10:54

Batch inserts using the shell

When using the shell, many times we want to insert a large number of documents programmatically. The most straightforward implementation, since we have a JavaScript shell, is to iterate through a loop, generating each document along the way and performing a write operation in every iteration in the loop like this:

> authorMongoFactory = function() {for(loop=0;loop<1000;loop++) {db.books.insert({name: "MongoDB factory book" + loop})}}
function () {for(loop=0;loop<1000;loop++) {db.books.insert({name: "MongoDB factory book" + loop})}}

In this simple example, we create an authorMongoFactory() method for an author who writes 1,000 books on MongoDB with a slightly different name for each one:

> authorMongoFactory()

This will result in 1,000 writes being issued to the database. This, while convenient, is significantly harder for the database to handle.

Instead, using a bulk write, we can issue a single database insert command with the 1,000 documents that we have prepared beforehand:

> fastAuthorMongoFactory = function() {
var bulk = db.books.initializeUnorderedBulkOp();
for(loop=0;loop<1000;loop++) {bulk.insert({name: "MongoDB factory book" + loop})}
bulk.execute();
}

The end result is the same as before, with 1,000 documents following this structure in our books collection:

> db.books.find()
{ "_id" : ObjectId("59204251141daf984112d851"), "name" : "MongoDB factory book0" }
{ "_id" : ObjectId("59204251141daf984112d852"), "name" : "MongoDB factory book1" }
{ "_id" : ObjectId("59204251141daf984112d853"), "name" : "MongoDB factory book2" }

{ "_id" : ObjectId("59204251141daf984112d853"), "name" : "MongoDB factory book999" }

The difference from the user's perspective lies in speed of execution and reduced strain on the database.

In the preceding example, we used initializeUnorderedBulkOp() for the bulk operation builder setup. The reason we did that is because we don't care about the order of insertions being the same as the order in which we add them to our bulk variable with the bulk.insert() command.

This makes sense when we can make sure that all operations are unrelated to each other or idempotent.

If we care about having the same order of insertions we can use initializeOrderedBulkOp(), changing the second line of our function to this:

var bulk = db.books.initializeOrderedBulkOp();
主站蜘蛛池模板: 乌鲁木齐县| 台北市| 奇台县| 迁安市| 十堰市| 苏尼特右旗| 新兴县| 朝阳市| 赣榆县| 峡江县| 望江县| 上饶市| 克山县| 梧州市| 长岛县| 长岛县| 锡林浩特市| 济源市| 兴国县| 威海市| 嵊州市| 通州市| 维西| 张家界市| 延吉市| 砚山县| 广河县| 石棉县| 吉木乃县| 龙游县| 贵港市| 错那县| 赤峰市| 新郑市| 鲁甸县| 若羌县| 锡林浩特市| 沈丘县| 永州市| 囊谦县| 益阳市|