title: Is it a good practice to build a collection cache?
body: <p>I'm experiencing with MongoDB with Node.js using the plugin <a href="https://github.com/christkv/node-mongodb-native" rel="nofollow"><code>node-mongodb-native</code></a>. A problem I'm experiencing is the amount of nested callbacks. I'm trying to simplify a few things by lessening the code required for a query.</p>
  
  <p>Instead of this ...</p>
  
  <pre><code>db.collection("test", function(err, collection) {
      collection.find(...).toArray(function(err, results) {
          // ...
      });
  });
  </code></pre>
  
  <p>... I was thinking of building an object which acts as a cache of collections so that the first callback is not necessary. I'm using the following code for building the object:</p>
  
  <pre><code>var collections = {};
  
  ["test", "foo"].forEach(function(name) {
      db.collection(name, function(err, coll) {
          collections[name] = coll;
      });
  });
  </code></pre>
  
  <p>With it, I'm able to clean up the first code snippet to:</p>
  
  <pre><code>collections.test.find(...).toArray(function(err, results) {
      // ...
  });
  </code></pre>
  
  <p>I was wondering whether this is a good practice. It works just fine, but I guess the callback of getting a collection is there for a reason. Does it make sense to build a collection cache as I'm doing now?</p>