Connection mongodb with nodejs

0

How do I connect to the mongo database with nodejs? I already consulted several sites but I can not do it correctly! Thanks for helping!

    
asked by anonymous 29.06.2018 / 18:19

1 answer

0

Use mongoose .

Here's an example of how to open the connection

var mongoose = require('mongoose');
global.db = mongoose.connect('mongodb://localhost:27017/neventos');
mongoose.connection.on('connected', function () {
 console.log('=====Conexão estabelecida com sucesso=====');
});
mongoose.connection.on('error', function (err) {
 console.log('=====Ocorreu um erro: ' + err);
});
mongoose.connection.on('disconnected', function () {
 console.log('=====Conexão finalizada=====');
}); 

In this link has a complete project that used mongoose to make the connection .

    
29.06.2018 / 18:32