I am encountering an issue with findOneAndUpdate operation on nodejs mongodb library, where I cannot tell if an update was done or not.
I wrote a query that can successfully update the document as expected, but value:null was returned after the update. Ultimately, I would like to distinguish between 3 cases:
- Document found, cannot be updated
- Document found and successfully updated
Document not found
const result = await collection.findOneAndUpdate( { caseId: caseId, list: {$ne: userId}, $expr: {$lt:[ {$size: '$list'}, 'maxLength']} }, { $push: {assignees: userId}}) return result.value;
A little explaination on the query:
I want to append userId
to the list
field of a document if
- caseId matches,
- the user is not currently in the
list
, - the size of the list is smaller than the
maxLength