Connecting to a MongoDB Database by Using Mongoose
Connecting to a MongoDB database by using Mongoose is very similar to using the connection string method discussed in Chapter 13, “Getting Started with MongoDB and Node.js.” It uses the connection string format and options syntax shown below:
connect(uri, options, [callback])
Note
The Mongoose library must be installed using npm install mongoose
to be able to use it in your Node.js applications.
The connect()
method is exported at the root level of the mongoose
module. For example the following code connects to the words
database on the localhost
:
var mongoose = require('mongoose');mongoose.connect('mongodb://localhost/words');
Get Node.js, MongoDB, and AngularJS Web 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.