Node.js Connect to MongoDB Using Mongoose


First, install Mongoose with:

npm install mongoose

Then, add it to server.js as dependencies:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

Next, create the database schema and the name of the collection:

var schemaName = new Schema({
    request: String,
    time: Number
}, {
    collection: 'collectionName'

Create a model and connect to the database:

var Model = mongoose.model('Model', schemaName);

Next, start MongoDB and run server.js using node server.js

To check if we have successfully connected to the database, we can use the events open, error from the mongoose.connection object.

var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
  // we're connected!