What is best way to handle global connection of Mongodb in NodeJs : <p>I using <a href="https://github.com/mongodb/node-mongodb-native" rel="noreferrer">Node-Mongo-Native</a> and trying to set a global connection variable, but I am confused between two possible solutions. Can you guys help me out with which one would be the good one?
1. Solution ( which is bad because every request will try to create a new connection.)</p>

<pre><code>var express = require('express');  
var app = express();  
var MongoClient = require('mongodb').MongoClient;  
var assert = require('assert');

// Connection URL
var url = '[connectionString]]';

// start server on port 3000
app.listen(3000, '0.0.0.0', function() {  
  // print a message when the server starts listening
  console.log("server starting");
});

// Use connect method to connect to the server when the page is requested
app.get('/', function(request, response) {  
  MongoClient.connect(url, function(err, db) {
    assert.equal(null, err);
    db.listCollections({}).toArray(function(err, collections) {
        assert.equal(null, err);
        collections.forEach(function(collection) {
            console.log(collection);
        });
        db.close();
    })
    response.send('Connected - see console for a list of available collections');
  });
});
</code></pre>

<ol start="2">
<li><p>Solution ( to connect at app init and assign the connection string to a global variable). but I believe assigning connection string to a global variable is a not a good idea.</p>

<p>var mongodb;
var url = '[connectionString]';
MongoClient.connect(url, function(err, db) {<br>
    assert.equal(null, err);
    mongodb=db;
    }
);</p></li>
</ol>

<p>I want to create a connection at the app initialization and use throughout the app lifetime.</p>

<p>Can you guys help me out? Thanks.</p>
