pablocattaneo
12/22/2018 - 5:41 PM

How to connect mongodb using mongoose?

How to connect mongodb using mongoose? #mongoose #mongodb

Source: https://www.udemy.com/the-complete-developers-guide-to-mongodb/learn/v4/t/lecture/6035440?start=0

const mongoose = require('mongoose')

mongoose.connect('mongodb://host_name/database_name', { useNewUrlParser: true }) // For example mongodb://localhost/users_test

mongoose.connection
  .once('open', () => {
    console.log('Good to go!')
  })
  .on('error', (error) =>{
    console.warn('Warning', error)
  })
  
  // Note that if the database not exist mongoose will create one for you