MongoDB is one of the most popular databases used along with Node.js. We need a driver to access Mongo from within a Node application. There are a number of Mongo drivers available, but MongoDB is among the most popular. To install the MongoDB module, run the below command -
npm install mongodb
Once installed, the given code snippet shows you how to create and close a connection to a MongoDB database.
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/mydb";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
console.log("Database created!");
db.close();
});
Solution
MongoDB is one of the most popular databases used along with Node.js. We need a driver to access Mongo from within a Node application. There are a number of Mongo drivers available, but MongoDB is among the most popular. To install the MongoDB module, run the below command -
npm install mongodb
Once installed, the given code snippet shows you how to create and close a connection to a MongoDB database.
need an explanation for this answer? contact us directly to get an explanation for this answer