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.
- If a document is found, but the update definition doesn’t have any $set operators just $setOnInsert what happens?
- Will the unmodified document be returned?
- Will an error be thrown?
- If I want to get back the inserted document, do I set ReturnDocument = After?
- 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.
Thanks