title: Auto-increment field in MongoDb with async bulk insertion
body: <p>I'm looking at this article about auto-incrementing fields in MongoDb. 
  <a href="http://docs.mongodb.org/manual/tutorial/create-an-auto-incrementing-field/" rel="nofollow">http://docs.mongodb.org/manual/tutorial/create-an-auto-incrementing-field/</a></p>
  
  <p>This seems good in theory, but doesn't work in my application where all DB calls are async. 
  Ps, Im using Mongoskin.
  Consider this: </p>
  
  <pre><code>var getNextSequence function (name) {
    db.counters.findAndModify({_id: 'invoiceNr'}, {}, {$inc: {seq: 1}}, {new: true}, function(err, doc) {
      if (err) {
        return next(err);
      }
      return doc.seq;
    });
  }; 
  
  var seq = getNextSequence('invoiceNr');
  console.log(seq);
  </code></pre>
  
  <p>This (Console.log) will of course not have any value when executed... </p>
  
  <p>This makes me thinking about nested callbacks instead (classic Node style): </p>
  
  <pre><code>db.counters.findAndModify({_id: 'invoiceNr'}, {}, {$inc: {seq: 1}}, {new: true}, function(err, doc) {
    if (err) {
      return next(err);
    }
    var seq = getNextSequence('invoiceNr');
    // Do something with the seq, like using it when inserting another document.
  });
  </code></pre>
  
  <p>This will work fine for one document, but I cant see how this would work if I make a bulk Insert (passing an array of several documents to insert). And I NEED bulk insertions. </p>
  
  <p>Going back to loops and single insertions doesnt seems like a good solutions. </p>
  
  <p>Do you have any good solution for this?</p>