简体   繁体   English

mongoose 在 findOneAndUpdate 更新之前访问当前值

[英]mongoose access current value before updating it inside findOneAndUpdate

I have the following code我有以下代码

let doc = await IssueModel.findOne({ content_id })
    IssueModel.findOneAndUpdate({
        content_id
    }, {
        $set: {
            assigned_user,
            status: doc.status==='done' ? 'done' : 'ongoing'
        }
    })

I'm looking for a shorter way to omit the first line, and access status current value before updating it inside the findOneAndUpdate , something similar to this maybe.我正在寻找一种更短的方法来省略第一行,并在更新findOneAndUpdate之前访问状态当前值,可能与此类似。 I want to update status only if it's different from 'done'.我只想更新与“完成”不同的状态

You can use this to save as well您也可以使用它来保存

let doc = await IssueModel.findOne({ content_id })
if(doc.status !== 'done') {
 doc.status = 'ongoing'
 await doc.save();
 return res.json({success: true})
}
res.json({success: true});

I have the following code我有以下代码

let doc = await IssueModel.findOne({ content_id })
    IssueModel.findOneAndUpdate({
        content_id
    }, {
        $set: {
            assigned_user,
            status: doc.status==='done' ? 'done' : 'ongoing'
        }
    })

I'm looking for a shorter way to omit the first line, and access status current value before updating it inside the findOneAndUpdate , something similar to this maybe.我正在寻找一种更短的方法来省略第一行,并在findOneAndUpdate中更新它之前访问状态当前值,可能与此类似。 I want to update status only if it's different from 'done'.只有当状态与“完成”不同时,我才想更新状态

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM