Node.js Connect to Mongodb Simple example to Connect mongoDB from Node.JS

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

MongoClient.connect('mongodb://localhost:27017/myNewDB',function (err,db) {
    if(err) 
        console.log("Unable to connect DB. Error: " + err)
    else 
        console.log('Connected to DB');

    db.close();
});

myNewDB is DB name, if it does not exists in database then it will create automatically with this call.



Got any Node.js Question?