Skip to content

Latest commit

 

History

History
35 lines (30 loc) · 920 Bytes

section24.1.md

File metadata and controls

35 lines (30 loc) · 920 Bytes

Section 24.1: Connect to MongoDB Using Mongoose

First, install Mongoose with:

  • npm install mongoose

Then, add it to server.js as dependencies:

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

// Next, create the database schema and the name of the collection:
let schemaName = new Schema({
  request: String,
  time: Number
  }, {
  collection: 'collectionName'
});

Create a model and connect to the database:

let 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.

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