NodeJS MongoDB Authentication

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

NodeJS and MongoDB are two parts of the MEAN and MERN stacks. NodeJS is a runtime environment that executes javascript outside the browser and MongoDB is a very popular NoSQL database. NodeJS works with almost any database technology and very often with MongoDB. There are many reasons for this such as there is no need for much well-structured scheme in MongoDB, there are no tables in MongoDB. Using NodeJS with MongoDB is easy and the very first step in combining NodeJs with MongoDB is setting up the connection between two. In this article, we will learn how to set up NodeJS MongoDB authentication.

NodeJS and MongoDB connection

The very first step is setting up the MongoClient. We need a reference of the MongoClient from MongoDB.

1
var MongoClient = require("mongodb").MongoClient;

Here we used the require keyword to include the MongoDB module. We got the reference of MongoClient in the MongoClient variable. The next step is to provide the reference to the local database. We can always provide reference to any MongoDB database. But here we will use the reference for the local database.

1
var url = "mongodb://localhost:27017/demoDB";

localhost:27017, this is the host and port of the database we wish to connect. demoDB is the name of the database. It will create a new database or it will switch to it if already exists.

Then, we can use the connect function to create a connection with MongoDB reference.

1
MongoClient.connect(url, function(err, db) {});

The connect function returns the reference to the database. The connect function has two parameters – url and a callback function. The url is that we specified earlier. The callback function has two parameters – err and db. This function will be called when the connection is completed. So basically, the connection happens in the background and when it is completed, this callback function is called.

One of the parameters of the callback function is err. This denotes the error that can occur during connection. We can use it to display an error if there is any.

1
2
3
4
5
if (err) {
  throw err;
} else {
  console.log("Database created succussfully!");
}

This piece of code will be included in the connect function. If there occurs an error while establishing the connection, it will throw an error. We can write the CRUD operation such as insertion in the connect function.

Here is the code for setting up connection between NodeJS and MongoDB.

1
2
3
4
5
6
7
8
9
10
11
var MongoClient = require("mongodb").MongoClient;

var url = "mongodb://localhost:27017/demoDB";

MongoClient.connect(url, function(err, db) {
  if (err) {
    throw err;
  } else {
    console.log("Database created succussfully!");
  }
});

Conclusion

We hoped you were able to follow along in this tutorial on how to set up authentication between with MongoDB from NodeJS. The full code snippet is below if you just want to copy the code. We showed how to use proper error handling so that you know whether or not your database was connected to or not and if it didn’t connect properly, you can debug from the error message logged.We hope you can apply what you’ve learned to your specific application. This is the tip of the iceberg so please check out our other articles on working with MongoDB.

Just the Code

1
2
3
4
5
6
7
8
9
10
11
var MongoClient = require("mongodb").MongoClient;

var url = "mongodb://localhost:27017/demoDB";

MongoClient.connect(url, function(err, db) {
  if (err) {
    throw err;
  } else {
    console.log("Database created succussfully!");
  }
});

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.