title: Mongoose asynchronous multiple save conflicts
body: <p>I've a big logic problem using Node/Mongoose/Socket.io ... Let's say I got a Server model which is often called at the same time in my application, some calls involve updating datas in the model.</p>
  
  <pre><code>    db.Server.findOne({_id: reference.server}).exec(function(error, server) {
  
          catches.error(error);
  
          if (server !== null) {
  
                server.anything = "ahah";
  
                server.save(function(error) { });
  
          }
  
      }
  </code></pre>
  
  <p>Sometimes, 2 people will call this at the same time, while the first person will save() the other guy could already have findOne the "server" and got the "old object" which isn't up-to-date and save() it.</p>
  
  <p>The big problem here is when the second guy will save() the "server" (the "old object") it will literally overwrite the changes of the first one ... You can imagine the big conflicts it will create on my application.</p>
  
  <p>I thought about changing all the save() methods to update() which get rid of the problem but at some point in the project it's very tricky to use the update() directly, and not as practical.</p>
  
  <p>Is there a way to "lock" the findOne() call while someone is updating it ? Like when you findOne() you also say "hey i will update this soon so don't let people find it right now" (with Mongoose, or even MongoDb)</p>
  
  <p>It's been a while i'm searching i don't find any answer :(</p>
  
  <p>Hope you understood my problem ;) thank you ! </p>