来看看 发表于 2018-10-25 09:45:40

Mongodb的Bulk Write 操作

第二条数据主键冲突,则只会插入一条数据  
> db.log.bulkWrite( [
  
...   { insertOne : { "document" : {"_id" : 4, "char" : "Dithras", "class" : "barbarian", "lvl" : 4 } } },
  
...   { insertOne : { "document" : {"_id" : 2, "char" : "Dithras", "class" : "barbarian", "lvl" : 4 } } },
  
...   { insertOne : { "document" : {"_id" : 5, "char" : "Dithras", "class" : "barbarian", "lvl" : 4 } } }
  
...   ],{ordered:true});
  
2017-04-10T17:48:37.960+0800 E QUERY    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:48
  
BulkWriteResult/this.toError@src/mongo/shell/bulk_api.js:336:24
  
Bulk/this.execute@src/mongo/shell/bulk_api.js:1173:1
  
DBCollection.prototype.bulkWrite@src/mongo/shell/crud_api.js:191:20
  
@(shell):1:1
  
> db.log.count();
  
4


页: [1]
查看完整版本: Mongodb的Bulk Write 操作