Proper way to re-user mongodb connection in node.js

By | July 4, 2013

I tried searching on the web and found no obvious code samples of how reuse the mongodb.connection object. This is what I have currently and hopefully the following snippet would help you out as well. If you do not think this is the right way, please leave a comment.

var app = express();
var mongodb = require('mongodb').MongoClient, format = require('util').format;

mongodb.connect('mongodb://127.0.0.1:27017/myday', function(err, db){
    app.get('/api/v1/put/:var1/:var2', function(req, res){
        collection = db.collection('collection');
        .. logic ...
    });
    app.listen(8000);
});

2 thoughts on “Proper way to re-user mongodb connection in node.js

  1. curtiscohnwjk.pen.io

    Having read this I thought it was very informative.

    I appreciate you finding the time and energy to put this short article together.
    I once again find myself personally spending a lot of time both reading and posting comments.
    But so what, it was still worth it!

    Reply
  2. video

    Thank you a lot for sharing this with all of us you actually know what
    you are talking about! Bookmarked. Please additionally consult with my website =).
    We could have a link alternate agreement between us

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.