1

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

1 Answer 1

1

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}); 
Sign up to request clarification or add additional context in comments.

1 Comment

yup, this is a better solution than mine, thanks. (though i'm still looking for an answer on whether it is possible or not to access the old value inside findOneAndUpdate)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.