【问题标题】:Mongoose findOneAndUpdate return old AND new valuesMongoose findOneAndUpdate 返回旧值和新值
【发布时间】:2021-01-02 01:44:28
【问题描述】:

我有这样的更新查询

Balances.findOneAndupdate({_id:  }, {$inc: {balance: 10}}, { new: true })

在我拥有的后期中间件中

schema.post('findOneAndUpdate', function (result) {
  result === updated document
})

如何在没有多个查询的情况下获取新旧(两者)值(用于记录)? 谢谢。

【问题讨论】:

标签: node.js mongodb mongoose


【解决方案1】:

如果有人需要,我通过交易解决。

const session = await dbInstance.startSession()
await session.startTransaction()
query ...
query ...
await session.commitTransaction()
session.endSession()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-26
    • 1970-01-01
    • 2016-11-18
    • 2019-05-01
    • 1970-01-01
    • 2016-05-08
    • 2019-07-12
    相关资源
    最近更新 更多