I have a Mongo collection that has two fields, let's say "name" and "randomString".
I want to create a random string for a name, only if it doesn't exist already. So the first request for { name: "SomeName" }
will result in saving e.g. { name: "someName", randomString: "abc" }
. The second request will do nothing.
Is there a mongo command for this? All I could find are things like findOneAndUpdate
, replaceOne
etc, who all support an optional "upsert" but their behavior on match is to update, I want the behavior on match to be do nothing.
I'm not looking for an if-then solution like in this question, as I have a race condition issue - I need to be able to get multiple requests simultaneously without updating the document or failing any of the requests.