【问题标题】:Why mongo db response value before update为什么更新前的mongodb响应值
【发布时间】:2021-07-30 21:12:43
【问题描述】:

我需要更新集合并返回更新的值。我的代码是:

const someFunction = async () => {
  const { value } = await dataStore.customerSettings.findOneAndUpdate(
            { organizationId }, 
            { $push: { archivedSuppliers: archivedSupplier } }
          );
  return value;
}

问题是值等于集合对象更新之前。为什么?以及如何获取更新值?

【问题讨论】:

标签: javascript mongodb mongodb-query


【解决方案1】:

http://mongodb.github.io/node-mongodb-native/4.0/interfaces/findandmodifyoptions.html#returnoriginal

findOneAndUpdate(filter, update, options, callback)

通过选项

returnOriginal: false

如果为 false,则返回更新后的文档而不是原始文档。默认为真。


await dataStore.customerSettings.findOneAndUpdate(
            { organizationId }, 
            { $push: { archivedSuppliers: archivedSupplier } },
            { returnOriginal: false } // add here
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多