从项目数组设置增量值

时间:2018-06-05 07:27:33

标签: node.js mongodb mongodb-query

如何更新MongoDB中的多个文档并按递增顺序设置元素的值?

我的文件如下

{
   "_id" :  ObjectId("5b162a31dfaf342dc44c920d")   
}
{
   "_id" :  ObjectId("5b162a31dfaf342dc44c920f")  
}
{
   "_id" :  ObjectId("5b162a31dfaf342dc44c920c")  
}

如何使用单个查询更新整个文档,以便我可以按递增顺序在每个字段中创建一个名为“order”的新元素,如下所示

{
   "_id" :  ObjectId("5b162a31dfaf342dc44c920d"),
   "order": 1
}
{
   "_id" :  ObjectId("5b162a31dfaf342dc44c920f"),
    "order": 2  
}
{
   "_id" :  ObjectId("5b162a31dfaf342dc44c920c"),
    "order": 3  
}

目前我使用以下方法解决问题

   for(let i = 0; i <= req.body.id.length;i++) {

            const queryOpts = {
                _id: ObjectId(req.body.id[i])
            };

            const updateOpts = {
                $set: {
                    'order': i + 1
                }
            };

        const dataRes = await req.db.collection('GalleryImage').updateOne(queryOpts, updateOpts); 

         if(i === req.body.id.length-1) {
                return commonHelper.sendResponseMessage(res, dataRes, {
                    _id: req.body.id
                }, moduleConfig.message.updateGalleryOrder);


            }

如果有比这更好的方法,那么如果有大量文件就不会是昂贵的操作?

1 个答案:

答案 0 :(得分:2)

使用bulkWrite()Array.map()构建语句:

try {
  let response = await req.db.collection('GalleryImage').bulkWrite(
    req.body.id.map((_id,order) => 
      ({ updateOne: {
       filter: { _id: ObjectId(_id) },
       update: {
         $set: { order: order+1 }
       }
      }})
    )
  );
 } catch(e) {
   // deal with any errors
 }

Array.map()在其第二个函数参数中处理数组元素的“索引”。因此,只需使用它来获取order并在所有语句中设置它。

不是用数据库n编写/回复,而只需要发生“一次”

除了自己介绍之外没有其他方法可以获得“序列”,但至少我们可以用“one”这样写,而不是几个。使用async/await语法时,请注意“捕获可能的错误”。

示例列表

const { MongoClient, ObjectID: ObjectId } = require('mongodb');

const uri = 'mongodb://localhost:27017';

const data = [
  "5b162a31dfaf342dc44c920d",
  "5b162a31dfaf342dc44c920f",
  "5b162a31dfaf342dc44c920c"
];

const log = data => console.log(JSON.stringify(data, undefined, 2));
(async function() {

  try {

    const client = await MongoClient.connect(uri);
    let db = client.db('test');

    // Set up
    await db.collection('gallery').removeMany({});
    await db.collection('gallery').insertMany(
      data.map(_id => ({ _id: ObjectId(_id) }))
    );

    // Update with indexes
    let response = await db.collection('gallery').bulkWrite(
      data.map((_id,idx) =>
        ({
          updateOne: {
            filter: { _id: ObjectId(_id) },
            update: { $set: { order: idx+1 } }
          }
        })
      )
    );

    log({ response });

    let items = await db.collection('gallery').find().toArray();
    log({ items });

    client.close();

  } catch(e) {
    console.error(e)
  } finally {
    process.exit()
  }

})()

输出

{
  "response": {
    "ok": 1,
    "writeErrors": [],
    "writeConcernErrors": [],
    "insertedIds": [],
    "nInserted": 0,
    "nUpserted": 0,
    "nMatched": 3,
    "nModified": 3,
    "nRemoved": 0,
    "upserted": [],
    "lastOp": {
      "ts": "6563535160225038345",
      "t": 18
    }
  }
}
{
  "items": [
    {
      "_id": "5b162a31dfaf342dc44c920d",
      "order": 1
    },
    {
      "_id": "5b162a31dfaf342dc44c920f",
      "order": 2
    },
    {
      "_id": "5b162a31dfaf342dc44c920c",
      "order": 3
    }
  ]
}

清楚地显示nMatched: 3nModified: 3正如预期的那样。