Bulk Write operation of Mongodb
This article comes from my own blog: www.wangerbao.com
Bulk Write Operations operation is a new feature of mongodb3.2. The syntax is as follows:
Db.collection.bulkWrite ([,...], {writeConcern:, ordered:})
Among them, ordered is a place to pay attention to, according to the official description:
The default is ture, that is, the data is inserted sequentially. If there is an error in the middle, the execution will not continue.
In the case of false, mongo inserts data concurrently, and errors in the middle have no effect on subsequent operations.
Examples are as follows
Initialize data, initialize 3 items
> db.log.count () 0 > db.log.bulkWrite ([. {insertOne: {"document": {"_ id": 1, "char": "Dithras", "class": "barbarian", "lvl": 4},... {insertOne: {"document": {"_ id": 2, "char": "Dithras", "class": "barbarian") "lvl": 4},... {insertOne: {"document": {"_ id": 3, "char": "Dithras", "class": "barbarian", "lvl": 4}.], {ordered:true}) {"acknowledged": true, "deletedCount": 0, "insertedCount": 3, "matchedCount": 0, "upsertedCount": 0, "insertedIds": {"0": 1, "1": 2, "2": 3} "upsertedIds": {} > db.log.count () three
Order default: true, if the second data primary key conflicts, only the first data will be inserted, and the total data amount is 4.
The second data primary key conflict Only one piece of data > db.log.bulkWrite ([. {insertOne: {"document": {"_ id": 4, "char": "Dithras", "class": "barbarian", "lvl": 4},... {insertOne: {"document": {"_ id": 2, "char": "Dithras", "class": "barbarian") is inserted. "lvl": 4},... {insertOne: {"document": {"_ id": 5, "char": "Dithras", "class": "barbarian", "lvl": 4}.], {ordered:true}) 2017-04-10T17:48:37.960+0800 E QUERY [thread1] BulkWriteError: write error at item 1 in bulk operation: BulkWriteError ({"writeErrors": [{"index": 1, "code": 11000 "errmsg": "E11000 duplicate key error collection: c_log.log index: _ id_ dup key: {: 2.0}", "op": {"_ id": 2, "char": "Dithras" "class": "barbarian", "lvl": 4}}], "writeConcernErrors": [], "nInserted": 1, "nUpserted": 0, "nMatched": 0 "nModified": 0, "nRemoved": 0, "upserted": []}) BulkWriteError@src/mongo/shell/bulk_api.js:372:48BulkWriteResult/this.toError@src/mongo/shell/bulk_api.js:336:24Bulk/this.execute@src/mongo/shell/bulk_api.js:1173:1DBCollection.prototype.bulkWrite@src/mongo/shell/crud_api.js:191:20@ (shell): 1:1 > db.log.count () four
Order is changed to false, and the first data primary key conflicts. If there is no problem with 2 or 3 items, the total data amount is 6.
> db.log.bulkWrite ([. {insertOne: {"document": {"_ id": 4, "char": "Dithras", "class": "barbarian", "lvl": 4},... {insertOne: {"document": {"_ id": 6, "char": "Dithras", "class": "barbarian") "lvl": 4},... {insertOne: {"document": {"_ id": 5, "char": "Dithras", "class": "barbarian", "lvl": 4}.], {ordered:false}) 2017-04-10T17:49:36.539+0800 E QUERY [thread1] BulkWriteError: write error at item 0 in bulk operation: BulkWriteError ({"writeErrors": [{"index": 0, "code": 11000 "errmsg": "E11000 duplicate key error collection: c_log.log index: _ id_ dup key: {: 4.0}", "op": {"_ id": 4, "char": "Dithras" "class": "barbarian", "lvl": 4}}], "writeConcernErrors": [], "nInserted": 2, "nUpserted": 0, "nMatched": 0 "nModified": 0, "nRemoved": 0, "upserted": []}) BulkWriteError@src/mongo/shell/bulk_api.js:372:48BulkWriteResult/this.toError@src/mongo/shell/bulk_api.js:336:24Bulk/this.execute@src/mongo/shell/bulk_api.js:1173:1DBCollection.prototype.bulkWrite@src/mongo/shell/crud_api.js:191:20@ (shell): 1:1 > db.log.count () six