【问题标题】:MongoDB update using limit and skipMongoDB 使用限制和跳过更新
【发布时间】:2019-01-19 22:50:12
【问题描述】:

在 Mongoose 中,我需要以最快的方式执行以下操作:

  1. 通过查询查找文档
  2. 使用limit() 和skip()
  3. 更新所有找到的记录中具有相同值的字段

你有什么建议?

【问题讨论】:

  • 是否要更新所有找到的记录中具有相同值的字段?
  • 是的,我用相同的值更新它们
  • 我可以推荐这位朋友的方法。 enter link description here

标签: mongodb mongoose


【解决方案1】:

您可以获取记录,然后将所有记录的 id 放入一个数组中,然后 使用$in更新它们

async function someAsyncFunction(){ 

    let foundData= await collection.find(query).skip().limit();
    let IDs=[];

    foundData.forEach(element=>{
     IDs.push(element._id);
       });

   return Collection.update(
                     {_id: { $in: IDs}}, 
                     { $set: {"fieldToUpdate": "value"}},
                     { multi: true }
                     );
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-18
    • 1970-01-01
    • 2021-11-04
    相关资源
    最近更新 更多