Getting Started with Node.js
Overview
This guide shows you how to create an application that uses the MongoDB Node.js driver to connect to a MongoDB cluster hosted on MongoDB Atlas. If you prefer to connect to MongoDB using a different driver or programming language, see our list of official drivers.
The Node.js driver is a library of functions that you can use to connect to and communicate with MongoDB.
MongoDB Atlas is a fully managed cloud database service that hosts your MongoDB deployments. You can create your own free (no credit card required) MongoDB Atlas deployment by following the steps in this guide.
Follow the steps in this guide to connect a sample Node.js application to a MongoDB Atlas deployment.
Download and Install
Install Node and npm
Ensure you have Node.js v16.20.1 or later and npm (Node Package Manager) installed in your development environment.
For information on how to install Node.js and npm, see downloading and installing Node.js and npm.
Create a Project Directory
In your shell, run the following command to create a
directory called node_quickstart
for this project:
mkdir node_quickstart
Run the following command to navigate into the project directory:
cd node_quickstart
Run the following command to initialize your Node.js project:
npm init -y
When this command successfully completes, you have a package.json
file in your node_quickstart
directory.
Install the Node.js Driver
Run the following command in your shell to install the driver in your project directory:
npm install mongodb@6.16
This command performs the following actions:
Downloads the
mongodb
package and the dependencies it requiresSaves the package in the
node_modules
directoryRecords the dependency information in the
package.json
file
After you complete these steps, you have Node.js and npm installed and a new project directory with the driver dependencies installed.
Note
If you run into issues on this step, ask for help in the MongoDB Community Forums or submit feedback by using the Rate this page tab on the right or bottom right side of this page.
Create a MongoDB Deployment
You can create a free tier MongoDB deployment on MongoDB Atlas to store and manage your data. MongoDB Atlas hosts and manages your MongoDB database in the cloud.
Create a Free MongoDB deployment on Atlas
Complete the Get Started with Atlas guide to set up a new Atlas account and load sample data into a new free tier MongoDB deployment.
After you complete these steps, you have a new free tier MongoDB deployment on Atlas, database user credentials, and sample data loaded in your database.
Note
If you run into issues on this step, ask for help in the MongoDB Community Forums or submit feedback by using the Rate this page tab on the right or bottom right side of this page.
Connect to MongoDB
Create your Node.js Application
Create a file to contain your application called index.js
in your
node_quickstart
project directory.
Copy and paste the following code into the index.js
file:
const { MongoClient } = require("mongodb"); // Replace the uri string with your connection string. const uri = "<connection string uri>"; const client = new MongoClient(uri); async function run() { try { const database = client.db('sample_mflix'); const movies = database.collection('movies'); // Query for a movie that has the title 'Back to the Future' const query = { title: 'Back to the Future' }; const movie = await movies.findOne(query); console.log(movie); } finally { // Ensures that the client will close when you finish/error await client.close(); } } run().catch(console.dir);
Assign the Connection String
Replace the <connection string uri>
placeholder with the
connection string that you copied from the Create a Connection String
step of this guide.
Run your Node.js Application
In your shell, run the following command to start this application:
node index.js
The output includes details of the retrieved movie document:
{ _id: ..., plot: 'A young man is accidentally sent 30 years into the past...', genres: [ 'Adventure', 'Comedy', 'Sci-Fi' ], ... title: 'Back to the Future', ... }
If you encounter an error or see no output, check whether you specified the
proper connection string in the index.js
file, and that you loaded the
sample data.
After you complete these steps, you have a working application that uses the driver to connect to your MongoDB deployment, runs a query on the sample data, and prints out the result.
Note
If you run into issues on this step, ask for help in the MongoDB Community Forums or submit feedback by using the Rate this page tab on the right or bottom right side of this page.
Next Steps
Congratulations on completing the quick start tutorial!
In this tutorial, you created a Node.js application that connects to a MongoDB deployment hosted on MongoDB Atlas and retrieves a document that matches a query.
Learn more about the MongoDB Node.js driver from the following resources:
Discover how to perform read and write operations in the CRUD Operations section.
See examples of frequently-used operations in the Usage Examples section.