I'm trying to implement a RESTful(ish) interface to a MongoDB database in Node.js. According to the docs, the the basic idiom is something like:
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
});
}
});
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.