简体   繁体   中英

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. 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. I want to update status only if it's different from 'done'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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