---
title: "Connect To A MongoDB Database Using Node.js"
description: "This tutorial walks through connecting a Node.js script to a MongoDB database, listing the cluster’s databases, and printing the results to the console. It begins with prerequisites: a supported Node.js version, the MongoDB Node.js Driver installed via npm, and a free MongoDB Atlas cluster loaded with sample data. The Atlas connection wizard adds the current IP address, creates a MongoDB user, and supplies a connection string for Node.js driver version 3.6 or later. The script imports MongoClient, defines an asynchronous main function, creates a client with the connection URI, and awaits client.connect(). Database calls sit inside try/catch, with a finally block closing the connection, and a listDatabases function retrieves and prints the database names before the script runs with node connection.js."
---

# Connect To A MongoDB Database Using Node.js

[MongoDB](https://yomu.fyi/company/mongodb) · Nov 3, 2026

**Type:** Tutorial

## Summary

This tutorial walks through connecting a Node.js script to a MongoDB database, listing the cluster’s databases, and printing the results to the console. It begins with prerequisites: a supported Node.js version, the MongoDB Node.js Driver installed via npm, and a free MongoDB Atlas cluster loaded with sample data. The Atlas connection wizard adds the current IP address, creates a MongoDB user, and supplies a connection string for Node.js driver version 3.6 or later. The script imports MongoClient, defines an asynchronous main function, creates a client with the connection URI, and awaits client.connect(). Database calls sit inside try/catch, with a finally block closing the connection, and a listDatabases function retrieves and prints the database names before the script runs with node connection.js.

## Context

The post is the first entry in a Quick Start series on using MongoDB with Node.js. It assumes a free Atlas cluster loaded with sample data and notes that the driver installed at the time of writing was version 3.6.4. It also mentions DeprecationWarnings around the URL string parser and Server Discover and Monitoring engine, which can be removed with the useNewUrlParser and useUnifiedTopology options.

## Approach / What changed

Install the MongoDB Node.js Driver, create an Atlas cluster and load sample data, add the current IP address and a MongoDB user, then copy the Node.js 3.6-or-later connection string. In connection.js, import MongoClient, define an asynchronous main function, set the URI with the user credentials and sample\_airbnb, instantiate MongoClient, and await client.connect(). Call listDatabases inside try/catch, close the client in a finally block, and invoke main with errors logged to the console.

## Takeaways

- The MongoDB Node.js Driver is installed with npm; at the time of writing the install produced driver version 3.6.4, and npm list mongodb shows the installed version.
- The Atlas connection wizard adds the current IP address to the IP Access List, creates a MongoDB user, and provides a connection string when Node.js and driver 3.6 or later are selected.
- DeprecationWarnings about the URL string parser and Server Discover and Monitoring engine can be removed by passing useNewUrlParser: true and useUnifiedTopology: true to MongoClient.

## FAQ

### What prerequisites are needed before connecting Node.js to MongoDB?

A supported Node.js version, the MongoDB Node.js Driver installed via npm, and a MongoDB database. The tutorial uses a free Atlas cluster loaded with sample data and notes the driver requires Node 4.x or greater.

### How should the connection be closed if a database call fails?

Wrap database calls in try/catch and put client.close() in a finally block. That ensures the connection to the cluster is closed even when an unexpected error occurs.

### What do the useNewUrlParser and useUnifiedTopology options address?

They remove DeprecationWarnings about the URL string parser and the Server Discover and Monitoring engine. The post shows instantiating MongoClient with both options set to true.

**Tags:** [Atlas](https://yomu.fyi/topic/atlas), [Database](https://yomu.fyi/topic/database), [MongoDB](https://yomu.fyi/topic/mongodb), [Node.js](https://yomu.fyi/topic/node-js)

- Source: [MongoDB](https://www.mongodb.com/company/blog/technical/quick-start-nodejs-mongodb-how-to-get-connected-to-your-database)
- Source URL: https://www.mongodb.com/company/blog/technical/quick-start-nodejs-mongodb-how-to-get-connected-to-your-database
- Ingested by Yomu: 2026-11-03T21:00:29.050Z

[Read original post](https://www.mongodb.com/company/blog/technical/quick-start-nodejs-mongodb-how-to-get-connected-to-your-database)
