javascript技巧

关注公众号 jb51net

关闭
首页 > 网络编程 > JavaScript > javascript技巧 > mongoose bulkWrite操作

mongoose之bulkWrite操作使用实例

作者:乾复道

这篇文章主要为大家介绍了mongoose之bulkWrite操作使用实例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

前言

在执行mongo操作时,有时候大家会觉得力不从心,比如:要给大量的数据更新,但是各个数据更新的内容不一样;需要批量创建大量数据; 以上操作,如果单纯使用findIOneAndUpdate或者save,首先是非常耗时、其次使用了大量的资源;

那么有没有什么好的方法去替代呢?

mongoose提供了bulkWrite批量操作防范,这个方法支持批量插入、更新和删除;

当然,不会有人以为批量操作就不能单独操作一条数据吧!

一: bulkWrite小小解释

mongoose关于bulkWrite的原文:mongoosejs.com/docs/api.ht…

操作语法:

db.collection.bulkWrite(
   [ <operation 1>, <operation 2>, ... ],
   {
      writeConcern : <document>,
      ordered : <boolean>
   }
)
参数类型是否必填描述
operationsarray必填bulkWrite() 写操作的数组。支持操作:insertOne、updateOne、updateMany、deleteOne、deleteMany、replaceOne
writeConcerndocument选填write concern 文档,省略则使用默认的 write concern,一般无用
orderedboolean选填表示mongod实例有序还是无序执行操作。默认值true。

关于ordered

综上所述,个人建议使用时配置为无序执行;各位可以按需求使用,后续会具体说明各项操作及返回值

二:具体操作

insertOne

插入单个文档到集合中;

db.collection.bulkWrite( [
   { insertOne : { "document" : {} } },
  { insertOne : { "document" : {} } }
] ,{ordered:fasle})
db.collection.bulkWrite( [
   { insertOne : { "document" : {} } },
  { insertOne : { "document" : {} } }
])

字段说明

updateOne

updateOne 更新集合中 filter 匹配的单个文档。如果匹配到多个文档 updateOne 仅更新第一个匹配到的文档;

db.collection.bulkWrite( [
   { updateOne :
      {
         "filter" : {name:"bulkWrite6"},
         "update" : {$set:{age:19}},
         "upsert" :true
      }
   }
],{ordered:fasle} )
db.collection.bulkWrite( [
   { updateOne :
      {
         "filter" : {name:"bulkWrite6"},
         "update" : {$set:{age:19}},,
         "upsert" :true
      }
   }
] )

字段说明

updateMany

updateMany 更新集合中所有匹配到的文档;

db.collection.bulkWrite( [
   { updateMany :
      {
         "filter" : {name:"bulkWrite6"},
         "update" : {$set:{age:19}},
         "upsert" :true
      }
   }
],{ordered:fasle} )
db.collection.bulkWrite( [
   { updateMany :
      {
         "filter" : {name:"bulkWrite6"},
         "update" : {$set:{age:19}},,
         "upsert" :true
      }
   }
] )

字段说明

deleteOne

deleteOne 删除集合中 filter 匹配到的单个文档。如果匹配到多个文档 deleteOne 只会删除一个匹配到的文档;

db.collection.bulkWrite([
   { deleteOne :  { "filter" : {age:19} } }
] )

字段说明

deleteMany

deleteMany 删除集合中 filter 匹配到的所有文档;

db.collection.bulkWrite([
   { deleteMany :  { "filter" : {age:19} } }
] )

字段说明

replaceOne

replaceOne 替换集合中 filter 匹配到的单个文档。如果匹配到多个文档 replaceOne 只会替换一个匹配到的文档;

db.collection.bulkWrite([
   { replaceOne :
      {
         "filter" :{age:19},
         "replacement" : {age:20},
         "upsert" :true
      }
   }
] )

字段说明

三:优点

四:以下需要注意

如下:

db.collection.bulkWrite(
   [
      { insertOne : <document> },
      { updateOne : <document> },
      { updateMany : <document> },
      { replaceOne : <document> },
      { deleteOne : <document> },
      { deleteMany : <document> }
   ],
   { ordered : false }
)

五:错误返回

错误返回值主要分为两种,ordered的有序和无序执行的错误返回, 以下述插入为例,第一项、第四项和第五项是不存在的,第二项和第三项的_id是已存在的

有序执行错误

db.collection.bulkWrite(
  [
    {
      insertOne: { document: { name: 'bulkWrite10' } }
    },
    {
      insertOne: { document: { _id: '6321a77b06e79133d99b926c', name: 'bulkWrite6' } }
    },
    {
      insertOne: { document: { _id: '63227cde0795b55523c0e8ac', name: 'bulkWrite7' } }
    },
    {
      insertOne: { document: { name: 'bulkWrite11' } }
    },
    {
      insertOne: { document: { name: 'bulkWrite12' } }
    }
  ],
  { ordered: true }
);

有序执行错误,返回值中只有一个错误的,但是insertedIds中index=2、3、4都没有执行

{
        "code": 11000,
        "writeErrors": [
            {
                "code": 11000,
                "index": 1,
                "errmsg": "E11000 duplicate key error collection: test_one.users index: _id_ dup key: { _id: ObjectId('6321a77b06e79133d99b926c') }",
                "op": {
                    "name": "bulkWrite6",
                    "age": 0,
                    "register_time": "2022-09-15T03:45:04.529Z",
                    "remark": null,
                    "vip": false,
                    "address": null,
                    "_id": "6321a77b06e79133d99b926c"
                }
            }
        ],
        "result": {
            "ok": 1,
            "writeErrors": [
                {
                    "code": 11000,
                    "index": 1,
                    "errmsg": "E11000 duplicate key error collection: test_one.users index: _id_ dup key: { _id: ObjectId('6321a77b06e79133d99b926c') }",
                    "op": {
                        "name": "bulkWrite6",
                        "age": 0,
                        "register_time": "2022-09-15T03:45:04.529Z",
                        "remark": null,
                        "vip": false,
                        "address": null,
                        "_id": "6321a77b06e79133d99b926c"
                    }
                }
            ],
            "writeConcernErrors": [],
            "insertedIds": [
                {
                    "index": 0,
                    "_id": "63229fc8afcdaac9bd268102"
                },
                {
                    "index": 1,
                    "_id": "6321a77b06e79133d99b926c"
                },
                {
                    "index": 2,
                    "_id": "63227cde0795b55523c0e8ac"
                },
                {
                    "index": 3,
                    "_id": "63229fc8afcdaac9bd268105"
                },
                {
                    "index": 4,
                    "_id": "63229fc8afcdaac9bd268106"
                }
            ],
            "nInserted": 1,
            "nUpserted": 0,
            "nMatched": 0,
            "nModified": 0,
            "nRemoved": 0,
            "upserted": []
        }
    }

无序执行错误

db.collection.bulkWrite(
  [
    {
      insertOne: { document: { name: 'bulkWrite10' } }
    },
    {
      insertOne: { document: { _id: '6321a77b06e79133d99b926c', name: 'bulkWrite6' } }
    },
    {
      insertOne: { document: { _id: '63227cde0795b55523c0e8ac', name: 'bulkWrite7' } }
    },
    {
      insertOne: { document: { name: 'bulkWrite11' } }
    },
    {
      insertOne: { document: { name: 'bulkWrite12' } }
    }
  ],
  { ordered: false }
);

无序执行错误,返回值中有两个报错,三个插入成功

{
        "code": 11000,
        "writeErrors": [
            {
                "code": 11000,
                "index": 1,
                "errmsg": "E11000 duplicate key error collection: test_one.users index: _id_ dup key: { _id: ObjectId('6321a77b06e79133d99b926c') }",
                "op": {
                    "name": "bulkWrite6",
                    "age": 0,
                    "register_time": "2022-09-15T03:47:51.976Z",
                    "remark": null,
                    "vip": false,
                    "address": null,
                    "_id": "6321a77b06e79133d99b926c"
                }
            },
            {
                "code": 11000,
                "index": 2,
                "errmsg": "E11000 duplicate key error collection: test_one.users index: _id_ dup key: { _id: ObjectId('63227cde0795b55523c0e8ac') }",
                "op": {
                    "name": "bulkWrite7",
                    "age": 0,
                    "register_time": "2022-09-15T03:47:51.976Z",
                    "remark": null,
                    "vip": false,
                    "address": null,
                    "_id": "63227cde0795b55523c0e8ac"
                }
            }
        ],
        "result": {
            "ok": 1,
            "writeErrors": [
                {
                    "code": 11000,
                    "index": 1,
                    "errmsg": "E11000 duplicate key error collection: test_one.users index: _id_ dup key: { _id: ObjectId('6321a77b06e79133d99b926c') }",
                    "op": {
                        "name": "bulkWrite6",
                        "age": 0,
                        "register_time": "2022-09-15T03:47:51.976Z",
                        "remark": null,
                        "vip": false,
                        "address": null,
                        "_id": "6321a77b06e79133d99b926c"
                    }
                },
                {
                    "code": 11000,
                    "index": 2,
                    "errmsg": "E11000 duplicate key error collection: test_one.users index: _id_ dup key: { _id: ObjectId('63227cde0795b55523c0e8ac') }",
                    "op": {
                        "name": "bulkWrite7",
                        "age": 0,
                        "register_time": "2022-09-15T03:47:51.976Z",
                        "remark": null,
                        "vip": false,
                        "address": null,
                        "_id": "63227cde0795b55523c0e8ac"
                    }
                }
            ],
            "writeConcernErrors": [],
            "insertedIds": [
                {
                    "index": 0,
                    "_id": "6322a06b9145a56d3b23fabe"
                },
                {
                    "index": 1,
                    "_id": "6321a77b06e79133d99b926c"
                },
                {
                    "index": 2,
                    "_id": "63227cde0795b55523c0e8ac"
                },
                {
                    "index": 3,
                    "_id": "6322a06b9145a56d3b23fac1"
                },
                {
                    "index": 4,
                    "_id": "6322a06b9145a56d3b23fac2"
                }
            ],
            "nInserted": 3,
            "nUpserted": 0,
            "nMatched": 0,
            "nModified": 0,
            "nRemoved": 0,
            "upserted": []
        }
    }

六:思考

个人经常使用bulkWrite的场景是:

bulkWrite是比save、update更为高级的写法,希望大家可以灵活运用。

引用

mongoose关于bulkWrite的原文:mongoosejs.com/docs/api.ht…

以上就是mongoose之bulkWrite操作使用实例的详细内容,更多关于mongoose bulkWrite操作的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文