Connect to MongoDB Using Mongoose

suggest change

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);
mongoose.connect('mongodb://localhost:27017/dbName');

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!
});

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Mongoose library:
* Connect to MongoDB Using Mongoose

Table Of Contents
1 npm
24 Mongoose library
41 cli
43 grunt
59 Hack
64 ES6
67 Redis
69 MongoDB
86 MongoDB
87 Lodash
91 CORS
105 N-API
108 Require