title: How to deal with connections and query queues in node.js node-mongodb?
body: <p>It is unclear to me how to work that connection and query the collection.
  Because sometimes the connection is already there and there is no problem. But when the connection needs to be established, collection queries aren't queued.
  You cannot make a new connection all the time either, because you can (and should) make only one.
  Seems like you need the collection query in a connection callback, but also stand-alone when the connection is already there.</p>
  
  <p>Ofcourse, this isn't undoable, but with all the blah and yaddah overhead, I imagine I am not doing it right.
  After all, this seems kinda <em>syntactic saccharin</em> to me.
  <strong>Where is the <a href="http://en.wikipedia.org/wiki/Syntactic_sugar" rel="nofollow">syntactic sugar</a>?</strong></p>
  
  <p>Here's a rudimentary example. Imagine a bunch of requests in the same second:</p>
  
  <pre><code>var util        = require('util'),
      mongo       = require('mongodb'),
      Server      = mongo.Server,
      Db          = mongo.Db,
      mServer     = new Server('localhost', 27017),
      mDb         = new Db('SomeDatabase', mServer),
      db;
  
  if (mDb._state == 'disconnected') {
      util.debug('MONGODB: connecting...');
      mDb.open(function(err, newDb) {
          if (!err) {
              db = newDb;
              // I cannot immediately work on a collection here, 
              // because it takes a moment for the db to connect.
          }
      });
  }
  else if (mDb._state == 'connecting') {
      // I need to catch this state because I cannot work on collections yet, 
      // but I cannot connect either because it's already connecting.
  }
  // else if (mDb._state == 'connected') {
  
  // Queries need to be performed after connection is established 
  // or when connection was already established by a previous request.
  db.createCollection(collection, function(err, collection) {
      if (!err) {
          collection.insert(record, {safe:true}, function(err, result) {
              util.debug('MONGODB: Inserted.');
          });
      }
  });
  
  // }
  </code></pre>
  
  <p>Now, the beginner guides I found are all about the callback on <code>mDb.open</code>, but this gives problems on successive requests when the node is already running. It refuses to open another connection. <strong>So, what is actually the correct way to play nice with mongodb in nodejs</strong> when a single launch callback isn't enough?</p>
  
  <p>I imagine a module that you pass queries in callback-form, which passes the callbacks around for you, either (1) to where it waits for the connection-callback, (2) directly to the collection, or (3) waiting for the connection, but too late for the connection-callback. But like I said, this is pure syntactic saccharin.</p>