title: Node.js, MongoDB - Inserting/updating multiple documents and sending a single response
body: <p>I'm trying to develop a synchronization server (think: SVN like) that accepts one or more documents (JSON string) from the client in one request (JSON stringified array of JS objects), inserts/updates them into mongodb and sends one response - which is a JSON string containing the insert/update status (and some more info like _id in mongo) of each document.</p>
  
  <p>If it was one document, i could have done one insert/update and in its callback, i could have sent the response. </p>
  
  <pre><code>collection.insert(_data, function(error, result) {
      if(error) res.send('error');
      else res.send(JSON.stringify({result: result}));
  });
  </code></pre>
  
  <p>But how to do this when i have more than one document. I could do insert/update one document in the previous one's callback. But i fear i'll end up with a dreadful ladder of code if i do that (i could do it in one function and recurse, yes).</p>
  
  <p>Any help will be greatly appreciated. BTW, i'm using this driver: <a href="http://mongodb.github.io/node-mongodb-native/" rel="nofollow">http://mongodb.github.io/node-mongodb-native/</a></p>
  
  <p>Note: I'm not looking at batch inserts or updates, as each document that is being processed needs individual processing. Some may need to be inserted, some updated, and then there is version number &amp; sync-status checking etc.</p>