FindOneAndUpdate behavior

I’m considering using the FindOneAndUpdate with Upsert = true to find a document and insert it if it doesn’t exist. I’m aware that I can specify when to return the document using the ReturnDocument property of FindOneAndUpdateOptions.

  1. If a document is found, but the update definition doesn’t have any $set operators just $setOnInsert what happens?
  2. Will the unmodified document be returned?
  3. Will an error be thrown?
  4. If I want to get back the inserted document, do I set ReturnDocument = After?
  5. But what if I need the existing document back? (I want either the existing or inserted document return).

I’m working on building a distributed lock manager to prevent different services/processes from stepping on each other when an existing document needs to be changed. For example, process 1 needs to replace the whole document while process 2 is only updating a part of the document.


Also, I’m restricted from using any .NET driver newer than 2.13.3.