title: Mongodb collection object caching in Node.js
body: <p>I'm trying to implement a RESTful(ish) interface to a MongoDB database in Node.js. According to the <a href="http://mongodb.github.com/node-mongodb-native/api-articles/nodekoarticle1.html" rel="nofollow">docs</a>, the the basic idiom is something like:</p>
  
  <pre><code>var mongo = require('mongodb'),
    Server = mongo.Server,
    Db = mongo.Db;
  
  var server = new Server('localhost', 27017, {auto_reconnect: true});
  var db = new Db('exampleDb', server);
  
  db.open(function(err, db) {
       if(!err) {
          db.collection('test', function(err, collection) {
              // do stuff with collection
          });
       }
  });
  </code></pre>
  
  <p>So there are three objects (server, db, and collection) involved in a basic DB access. My question is which of these can/should be loaded at startup and cached somewhere to be reused for every http request, and which must/should be recreated per request. My assumption is that it is okay for the server and db objects to be long lived, but I'm less sure about the collection object.</p>