Lesson 2: Connecting to an Atlas Cluster in Node.js Applications / Learn

Press O for more options
6:14

Video Transcript (English)

Hi! In this video, you will learn how to install the official MongoDB Node.js driver by using the npm package manager.

You’ll also learn how to connect a MongoClient instance in a Node.js application to an Atlas cluster by using a connection string.

Finally, you will learn how to instantiate a single MongoClient that can be reused across all database requests.

Let’s get started by using the npm package manager to install the MongoDB Node.js driver.

To follow along with this demo, create a new Node.js console app by creating a folder called “test” - Then initialize it as a Node.js project by using the following command: `npm init`

You will likely get an error if you don’t already have npm installed globally.

Check out the official npm documentation to ensure that you have Node.js and Node package manager (npm) installed.

If you're using a different IDE, or if you’re using a text editor, the experience will be slightly different, but the code will be the same.

Once your app is ready, open your app in your favorite code editor. We’ll use VS Code for this demo.

To install the driver, we’ll use npm, which you can execute from the command line.

Open the terminal in VS Code.

Next, install the driver and its dependencies by running the following command: `npm install mongodb`

The execution of this command may take a few seconds, depending on your Internet connection. Once completed, it will have downloaded the MongoDB package from the npm registry into the node_modules directory of your project.

Now that the driver is installed, let’s retrieve our connection string so that we can test connecting with the driver.

Log in to your Atlas account.

On the project dashboard, navigate to the Database tab.

You should see the clusters for your selected project. Next, click the Connect button.

This should bring up this screen.

We want to connect to our application, so select that option.

This brings up another window where we need to select the Node.js driver.

We can also choose the version that we want to use. For this example, we’ll use the latest version available.

Now let’s copy the connection string by clicking this icon.

For this example, the username is our database user; however, the password is not included. You’ll need to replace the with the password for the database user.

Also, make sure that any optional parameters are URL encoded.

Now that we've retrieved our connection string, we can use it to connect to our Atlas cluster with the Node.js driver.

Start by creating a new file called

Atlas_uri.js at the root of the project by using the touch command.

In this file, add the following code.

Don’t forget to swap out the password. The cluster name and username will already be included.

Now we need to load our URI configuration into our application. To do this, we’ll import the atlas_uri module into the entry point for the application.

In the root directory of our Node.js app, create a file called app.js.

In this file, import the MongoClient by using this code.

Then, we’ll import our database connection string by using this code.

To test that it’s being imported correctly, let’s add this line: console.log(uri)

Now we’ll run this script in the terminal to check it.

Go to the terminal and run the following command: `node app`

If it’s successful, you should see the URI in the console.

Next, we’ll use the MongoClient object to initiate the connection with the database.

You’ll notice that we’re passing in the imported URI to the MongoClient class and assigning it to a variable called client.

Now we can connect to the database.

We need to identify the database that we’re using as well as the collection. To do so, add the following line of code.

Next, let’s create our `connectToDatabase` function by using the code shown here:

We’ll also create a `main()` function that executes this `connectToDatabase` function, and then call it.

Save your work, and then run the app again.

This time, you should get a success message in the console that says: “Connected to the bank database.”

Excellent! It looks like we’re connected!

Well done!

Note that an application should use a single MongoClient instance for the life of the application, to be reused across all database requests. This is because creating MongoClients is resource intensive.

Creating a new MongoClient for each database request will affect the application’s performance.

Congratulations! Now you know how to connect to an Atlas cluster from your Node.js application.

Here are some key takeaways from this video:

Use the npm package manager to install the MongoDB Node.js driver.

We need a valid connection string to connect ourNode.js application to the MongoDB database via a driver.

The connection string is available in the Atlas dashboard. And

An application must use a single MongoClient instance for all database requests, for the life cycle of the application.