title: How to insert new document only if it doesn't already exist in MongoDB
body: <p>I have a collection of users with the following schema:</p>
  
  <pre><code>{
  _id:ObjectId("123...."),
  name:"user_name",
  field1:"field1 value",
  field2:"field2 value",
  etc...
  }
  </code></pre>
  
  <p>The users are looked up by the user.name, which must be unique. When a new user is added, I first perform a search and if no such user is found, I add the new user document to the collection. The operations of searching for the user and adding a new user, if not found, are not atomic, so it's possible, when multiple application servers are connect to the DB server, for two add_user requests to be received at the same time with the same user name, resulting in no such user being found for both add_user requests, which in turn results with two documents having the same "user.name". In fact this happened (due to a bug on the client) with just a single app server running NodeJS and using Async library. </p>
  
  <p>I was thinking of using findAndModify, but that doesn't work, since I'm not simply updating a field (that exists or doesn't exist) of a document that already exists and can use upsert, but want to insert a new document only if the search criteria fails. I can't make the query to be not equal to "user.name", since it will find other users.</p>