asma
asma

Reputation: 629

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'.

Upvotes: 1

Views: 579

Answers (1)

Indraraj26
Indraraj26

Reputation: 1976

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});

Upvotes: 1

Related Questions