DEV Community

loading...

#2 Installing MongoDB Locally & connecting to MongoDB

rittwickbhabak profile image Rittwick Bhabak ・1 min read
  1. npm init -- to track the dependencies.
  2. Install mongoose npm install mongoose --save
  3. Connecting to database: Mongoose do not automatically knows about database. we've to tell it explicitly.
//connection.js
const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/testaroo');

mongoose.connection.once('open', function(){
    console.log('Connection is made');
}).on('error', function(error){
    console.log('Connection error', error);
})
Enter fullscreen mode Exit fullscreen mode

Discussion

pic
Editor guide