Navigation

MongoDB Node.js Driver

Introduction

The official MongoDB Node.js driver provides both callback-based and Promise-based interaction with MongoDB, allowing applications to take full advantage of the new features in ES6. The 2.x series of the driver is powered by a brand new core driver and BSON library.

Take the free online course taught by MongoDB

../../_images/M220JS_hero.jpg

M220JS: MongoDB for JavaScript Developers

Learn the essentials of Node.js application development with MongoDB.

Installation

The recommended way to get started using the Node.js 2.0 driver is by using NPM (Node Package Manager) to install the dependency in your project.

After you’ve created your project with npm init, you can install the MongoDB driver and its dependencies with the command:

npm install mongodb --save

This will download the MongoDB driver and add a dependency entry in your package.json file.

Connect to MongoDB Atlas

To connect to a MongoDB Atlas cluster, use the Atlas connection string for your cluster:

const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://<username>:<password>@<your-cluster-url>/test?retryWrites=true";
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
  const collection = client.db("test").collection("devices");
  // perform actions on the collection object
  client.close();
});

See Connect to MongoDB for more ways to connect.

Compatibility

MongoDB Compatibility

The following compatibility table specifies the recommended version(s) of the MongoDB Node.js driver for use with a specific version of MongoDB.

The first column lists the driver version(s).

Node.js Driver MongoDB 4.0 MongoDB 3.6 MongoDB 3.4 MongoDB 3.2 MongoDB 3.0 MongoDB 2.6
>= 3.1
>= 3.0  
>=2.2.12    
>=2.0.14        
>=1.4.29        

The driver does not support older versions of MongoDB.

Language Compatibility

The following compatibility table specifies the recommended version(s) of the MongoDB Node.js driver for use with a specific version of Node.js.

The first column lists the driver version(s).

Node.js Driver Node.js v0.8.X Node.js v0.10.X Node.js v0.12.X Node.js v4.X.X Node.js v6.X.X Node.js v8.X.X Node.js v10.x.x
3.1.X      
3.0.X      
2.2.X  
2.1.X  
2.0.X  
>=1.4.18        
1.4.X          

How to get help