Reference the Mongoose Database Connection

Mongoose is an ODM for Node.js that connects to MongoDb. I've found Mongoose to be really useful in handling Models and REST. However, I ran into the need to connect directly to MongoDb rather that working through the Models. Here's the code to reference Mongooses Db so you don't have to connect twice.

// Database
var connection = mongoose.connect('mongodb://username:password@localhost/db_name');
var db = connection.connections[0].db;
comments powered by Disqus

© Balanced Scale Media, LLC. All right reserved.