Q:

How does node JS connect to MongoDB database?

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)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now