Accessing MongoDB outside of connection callback
<p>I know that similar questions have been asked, but no one actually shows code that does this, they only link to pages that also don't show code. </p>

<p>Anyway, basically my node server receives data on a socket.io event. I want that data to go into MongoDB. The problem is that all the code examples I've seen for mongo only manipulate the db inside the <code>MongoClient.connect</code> callback, using the <code>db</code> object.</p>

<p>Since, I will be getting a lot of this data, I don't want to initialize over and over again.</p>

<p>What I need is effectively this:</p>

<pre><code>MongoClient.connect(("mongodb://" + process.env.IP + ":27017/feedback"),
function(err, db) { ... });
</code></pre>

<p>And then later:</p>

<pre><code>socket.on('data', function (data) {
    db.doStuff();
});
</code></pre>