Q:

How does node JS connect to MongoDB database?

belongs to collection: Node.js programming Exercises

0

How does node JS connect to MongoDB database?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

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();
});

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

How do you zip a file in node.js?... >>
<< How to read a file line by line using node.js ?...