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.

    Take the free online course taught by MongoDB

    /figures/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 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&w=majority";
    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

    Node.js DriverMongoDB 4.2MongoDB 4.0MongoDB 3.6MongoDB 3.4MongoDB 3.2MongoDB 3.0MongoDB 2.6
    >= 3.5
    >= 3.4
    >= 3.3
    >= 3.2.1
    >= 3.1
    >= 3.0
    >=2.2.12
    >=2.0.14
    >=1.4.29

    The driver does not support older versions of MongoDB.

    Language Compatibility

    Node.js DriverNode.js v0.8.XNode.js v0.10.XNode.js v0.12.XNode.js v4.X.XNode.js v6.X.XNode.js v8.X.XNode.js v10.x.xNode.js v12.x.x
    3.5.X
    3.4.X
    3.3.X
    3.2.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