Connecting To a Database

suggest change

To connect to a MongoDB database from a Node.js application, use Mongoose.

Install Mongoose from the root of your application:

npm install mongoose

Next we connect to the database.

var mongoose = require('mongoose');

//connect to the test database running on default mongod port of localhost  
mongoose.connect('mongodb://localhost/test');
//Connecting with custom credentials
mongoose.connect('mongodb://USER:PASSWORD@HOST:PORT/DATABASE');


// maxPoolSize is the pool option in current Mongoose (the old server.poolSize form is ignored).
// connect() returns a promise. The callback argument was removed in Mongoose 6.
mongoose.connect('mongodb://localhost:27017/consumers', { maxPoolSize: 50 })
    .then(function () {
        console.log('Connected to the database');
    })
    .catch(function (err) {
        console.log('error in this');
        console.log(err);
    });

Feedback about page:

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



Table Of Contents