.. include:: /includes/unicode-checkmark.rst

======================
MongoDB Node.js Driver
======================

.. default-domain:: mongodb

.. contents:: On this page
   :local:
   :backlinks: none
   :depth: 1
   :class: twocols

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.

- :node-docs:`Tutorials <tutorials/main/>`

- :node-docs:`Usage Guide <reference/main/>`

- :node-docs:`API Reference <api/>`

- :node-docs:`Changelog <upgrade-migration/main/>`

- `Source Code <https://github.com/mongodb/node-mongodb-native>`__


Take the free online course taught by MongoDB
---------------------------------------------

.. list-table::

   * - .. cssclass:: bordered-figure
       .. figure:: /figures/M220JS_hero.jpg

     - `M220JS: MongoDB for JavaScript Developers <https://university.mongodb.com/courses/M220JS/about>`__
        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) <https://www.npmjs.com/>`__ 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:

.. code-block:: sh

   npm install mongodb --save

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


Connect to MongoDB Atlas
------------------------

.. include:: /includes/atlas-connect-blurb.rst

.. code-block:: javascript

   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 :node-docs:`Connect to MongoDB <tutorials/connect/>`
for more ways to connect.


Compatibility
-------------

MongoDB Compatibility
~~~~~~~~~~~~~~~~~~~~~

.. include:: /includes/mongodb-compatibility-table-node.rst

.. include:: /includes/older-server-versions-unsupported.rst

Language Compatibility
~~~~~~~~~~~~~~~~~~~~~~

.. include:: /includes/extracts/node-driver-compatibility-matrix-language.rst

.. include:: /includes/language-compatibility-table-node.rst

.. include:: /includes/help-links-node.rst
