'MongoDB Node.js driver: How to find out if findOneAndUpdate actually found a document and updated it?

I'm trying to use the Collection method findOneAndUpdate of MongoDB's native Node.js driver to find and update a document in one single operation.

I then need to perform some logic that depends on if the document was successfully found and updated or not.

According to the driver's documentation, findOneAndUpdate returns a ModifyResult. This has the field ok with expected values being either 0 or 1. So this seems to be promising.

However, what meaning does ok = 0 or ok = 1 have?

  • Is ok = 0 "I looked for documents according to your filter, but found none, so did not update any"?
  • Is ok = 0 "I failed to look for documents because of an error"?
  • Is ok = 0 "I looked for documents according to your filter and found one but failed to update it?
  • Is ok = 1 "I looked for documents according to your filter, and may or may not have found some - and if I found one, I did update it"?
  • Is ok = 1 "I looked for documents according to your filter, and definitely found one and updated it"?


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source