Chapter 2. Establishing a Database Connection

We are going to look at the two methods Mongoose uses to connect to databases, mongoose.connect and createConnection. We will also go through various configuration options.

By the end of this chapter, you will understand how to best use the different Mongoose connection methods to meet your needs. We will also have added a connection to the MongoosePM example project.

Mongoose default connection

The way to set the default connection to a MongoDB database via Mongoose is nice and easy, using the mongoose.connect method.

var dbURI = 'mongodb://localhost/mydatabase';
mongoose.connect(dbURI);

This will open a Mongoose connection to the Mongo database mydatabase, running on the server localhost. If established ...

Get Mongoose for Application Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.