Mongodb的Bulk Write 操作
admin
2023-01-26 05:00:09
0

本文来自与自己的博客:www.wangerbao.com

Bulk Write Operations操作是mongodb3.2的新增功能,语法如下:

db.collection.bulkWrite(
   [ , ... ],
   {
      writeConcern : ,
      ordered : 
   }
)

其中ordered是个需要注意的地方,根据官方描述:

  1. 默认是ture,也就是按照顺序插入数据,如果中间出现错误则不会在继续执行

  2. 如果是false,则mongo会采用并发的方式插入数据,中间出现错误对后续操作无影响

事例如下

  • 初始化数据,初始化3条

  • > 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();
    3
  • order默认:true,第二条数据主键冲突,则只会插入第一条数据,数据总量为4

  • 第二条数据主键冲突,则只会插入一条数据
    > 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    [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: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
  • order修改为false,第一条数据主键冲突,2、3条没问题,则数据总量为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: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();
    6


相关内容

热门资讯

美威胁袭伊基础设施,伊朗军方:... △当地时间21日,船只在霍尔木兹海峡航行伊朗武装部队哈塔姆安比亚中央总部当地时间22日发布声明称,美...
扑翼机器人问世:像潜水鸟一样在... 潜鸟、海鸥、海鹦和海燕,是自然界中约100种既能飞行又能游泳的鸟类。这些潜水鸟能够俯冲入水追逐猎物,...
当科学家走进西部脑谷:这里有望... 7月22日,2026天府科学家创新发展大会暨成都科产融合对接活动锦江经开区脑机接口专场在锦江区成功举...
女子身份证丢失后名下出现“吸毒... 四川籍的庞女士定居浙江宁波多年,她怎么也没想到,因丢失身份证一事,竟让她陷入长达7年“自证清白”的困...
省农科院成功研发我省首个高粱全... 近日,记者从省农科院获悉,该院作物资源研究所、农业生物技术研究所联合石家庄博瑞迪生物,成功研制出省内...
消息称AMD与Anthropi... IT之家 7 月 22 日消息,据华尔街日报消息,AMD 与 Anthropic 达成了一项价值数百...
独家!苹果iPhone18系列... 7月22日,中国证券报记者从产业链人士处独家获悉,进入7月,苹果iPhone 18系列手机已在量产,...
29岁的党鑫蕊倒在广西泥泞山路... 7月11日晚一通叮嘱,成了父子最后的对话;几天后父亲在殡仪馆见到一身泥的儿子,才知道“他走得有多难。...
时隔一年,英国国防大臣斯特里廷... 一年前,当时还是英国卫生大臣的韦斯·斯特里廷(Wes Streeting)曾公开指责以色列犯下战争罪...
微软与Mistral宣布扩大战... 【CNMO科技消息】7月22日,据韩媒报道,微软与法国AI企业Mistral正式宣布大幅扩大双方战略...