Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion config/redirects
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
define: prefix docs/drivers/node
define: base https://www.mongodb.com/${prefix}
define: versions v3.6 v3.7 v4.0 v4.1 v4.2 v4.3 v4.4 v4.5 v4.6 v4.7 v4.8 v4.9 v4.10 v4.11 v4.12 v4.13 v5.0 master
define: versions v3.6 v3.7 v4.0 v4.1 v4.2 v4.3 v4.4 v4.5 v4.6 v4.7 v4.8 v4.9 v4.10 v4.11 v4.12 v4.13 v5.0 v5.1 master

symlink: current -> master

Expand All @@ -11,3 +11,10 @@ raw: ${prefix}/stable -> ${base}/current/
[*-master]: ${prefix}/${version}/fundamentals/connection/lambda/ -> ${base}/${version}/fundamentals/connection/
[v4.7-*]: ${prefix}/${version}/fundamentals/csfle -> ${base}/${version}/fundamentals/encrypt-fields/
[*-v4.6]: ${prefix}/${version}/fundamentals/encrypt-fields -> ${base}/${version}/fundamentals/csfle/

[*-v5.0]: ${prefix}/${version}/quick-start/connect-to-mongodb/ -> ${base}/${version}/quick-start/
[*-v5.0]: ${prefix}/${version}/quick-start/create-a-connection-string/ -> ${base}/${version}/quick-start/
[*-v5.0]: ${prefix}/${version}/quick-start/create-a-deployment/ -> ${base}/${version}/quick-start/
[*-v5.0]: ${prefix}/${version}/quick-start/download-and-install/ -> ${base}/${version}/quick-start/
[*-v5.0]: ${prefix}/${version}/quick-start/next-steps/ -> ${base}/${version}/quick-start/

2 changes: 2 additions & 0 deletions snooty.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ toc_landing_pages = [
"/fundamentals/connection",
"/fundamentals/crud",
"/usage-examples",
"/quick-start",
]
sharedinclude_root = "https://raw.githubusercontent.com/10gen/docs-shared/main/"

[constants]
version = "5.0"
package-name-org = "mongodb-org"
api = "https://mongodb.github.io/node-mongodb-native/{+version+}"
min-node-version = "v12"
mongosh = "``mongosh``"
driver = "node"
driver-long = "MongoDB Node.js driver"
Expand Down
Binary file modified source/includes/figures/atlas_connection_select_cluster.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions source/includes/quick-start/troubleshoot.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.. note::

If you run into issues on this step, ask for help in the
:community-forum:`MongoDB Community Forums <tag/node-js/>`
or submit feedback using the :guilabel:`Share Feedback`
tab on the right or bottom right side of this page.
30 changes: 11 additions & 19 deletions source/index.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ MongoDB Node Driver

.. _node-driver-landing:

.. default-domain:: mongodb

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

.. toctree::
:titlesonly:
:maxdepth: 1
Expand All @@ -32,12 +28,11 @@ You can add the driver to your application to work with MongoDB
in JavaScript. Download it using `npm <https://www.npmjs.com/>`__
or set up a runnable project by following our Quick Start guide.


Quick Start
-----------

Learn how to establish a connection to MongoDB Atlas and begin
working with data in the :doc:`Quick Start </quick-start>` section.
working with data in the step-by-step :doc:`Quick Start </quick-start>`.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a blocker, but can we make this into a :ref: tag instead of a :doc: while we're making changes in here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing that out! Since this PR isn't initially going to be backported, I think it might be better to leave alone and address somewhere else.


Quick Reference
---------------
Expand All @@ -60,8 +55,8 @@ API
---

For detailed information about classes and methods in the MongoDB
Node.js driver, see the `MongoDB Node.js driver API documentation
<{+api+}>`__ .
Node.js driver, see the `{+driver-long+} API documentation
<{+api+}>`__.

FAQ
---
Expand All @@ -73,15 +68,14 @@ section.
Issues & Help
-------------

Learn how to report bugs, contribute to the driver, and find
additional resources for asking questions and receiving help in the
Learn how to report bugs, contribute to the driver, and to find help in the
:doc:`Issues & Help </issues-and-help>` section.

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

For the compatibility charts that show the recommended Node.js
Driver version for each MongoDB Server version, see the
For the compatibility tables that show the recommended {+driver-short+}
version for each MongoDB Server version, see the
:doc:`Compatibility </compatibility>` section.

What's New
Expand All @@ -94,19 +88,18 @@ Learn
-----

Visit the Developer Hub and MongoDB University to learn more about the
MongoDB Node.js driver.
{+driver-short+}.

Developer Hub
~~~~~~~~~~~~~

The Developer Hub provides tutorials and social engagement for
developers.
The Developer Hub provides tutorials and social engagement for developers.

To learn how to use MongoDB features with the Node.js driver, see the
`How To's and Articles page <https://developer.mongodb.com/learn/?content=Articles&text=Node.js>`_.
`How To's and Articles page <https://www.mongodb.com/developer/search/?s=Node.js>`__.

To ask questions and engage in discussions with fellow developers using
the Node.js driver, see the `forums page <https://developer.mongodb.com/community/forums/tag/node-js>`_.
the {+driver-short+}, see the `Developer Community forums <https://www.mongodb.com/community/forums/tag/node-js>`__.

MongoDB University
~~~~~~~~~~~~~~~~~~
Expand All @@ -118,8 +111,7 @@ Take the free online course taught by MongoDB instructors

.. list-table::

* - .. cssclass:: bordered-figure
.. figure:: /includes/figures/M220JS_hero.jpg
* - .. figure:: /includes/figures/M220JS_hero.jpg
:alt: Banner for the MongoDB University Node.js Course

- `Using MongoDB with Node.js <https://learn.mongodb.com/learning-paths/using-mongodb-with-nodejs-y13d>`_
Expand Down
210 changes: 18 additions & 192 deletions source/quick-start.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,211 +4,37 @@
Quick Start
===========

.. default-domain:: mongodb

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

Overview
--------

This guide shows you how to create an application that uses the
{+driver-long+} 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 :driver:`list of official drivers <>`.

The {+driver-short+} is a library you can use to connect to and communicate
with MongoDB.
The {+driver-short+} 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 servers. You can get started with your own free (no credit card
required) MongoDB instance with this guide.

Follow the steps below to connect a sample Node.js application to a MongoDB
instance on MongoDB Atlas.

Set up Your Project
-------------------

Install Node and npm
~~~~~~~~~~~~~~~~~~~~

Ensure you have Node.js v12 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 <https://docs.npmjs.com/downloading-and-installing-node-js-and-npm>`__.


Create the Project
~~~~~~~~~~~~~~~~~~

First, in your shell, create a directory for your project:

.. code-block:: bash

mkdir node_quickstart

Then, navigate into that directory:

.. code-block:: bash

cd node_quickstart

Next, initialize your project:

.. code-block:: bash

npm init -y

Add MongoDB as a Dependency
~~~~~~~~~~~~~~~~~~~~~~~~~~~

Install the {+driver-short+}:

.. code-block:: bash

npm install mongodb@{+version+}

This command performs the following actions:

- Downloads the ``mongodb`` package and the dependencies it requires
- Saves the package in the ``node_modules`` directory
- Records the dependency information in the ``package.json`` file

At this point, you are ready to use the {+driver-short+} with your
application.

.. _node-quickstart-connect-cluster:

Create a MongoDB Cluster
~~~~~~~~~~~~~~~~~~~~~~~~

.. procedure::
:style: connected

.. step:: Create a Free Tier Cluster in Atlas

Create a free tier MongoDB cluster on MongoDB Atlas to store and manage
your data. MongoDB Atlas hosts and manages your MongoDB database in the
cloud. Complete the :atlas:`Get Started with Atlas </getting-started?tck=docs_driver_nodejs>`
guide to set up a new Atlas account, a free tier cluster (a shared
MongoDB instance) and load sample data into your cluster.

.. step:: Connect to your Cluster

You can connect to your MongoDB cluster by providing a
**connection string** which instructs the driver on where and how to
connect. The connection string includes information on the hostname
or IP address and port of your cluster, the authentication mechanism,
user credentials when applicable, and other connection options.

To connect to an instance or cluster not hosted on Atlas, see
:ref:`Other Ways to Connect to MongoDB <node-other-ways-to-connect>`.

To retrieve your connection string for the cluster you created in
the previous step, log into your Atlas account and navigate to the
:guilabel:`Database` section and click the :guilabel:`Connect` button
for the cluster that you want to connect to as shown below.

.. figure:: /includes/figures/atlas_connection_select_cluster.png
:alt: The connect button in the clusters section of the Atlas UI

Proceed to the :guilabel:`Connect Your Application` section and select
the {+driver-short+}. Select the :guilabel:`Connection String Only` tab
and click the :guilabel:`Copy` button to copy the connection string to
your clipboard as shown below.

.. figure:: /includes/figures/atlas_connection_copy_string_node.png
:alt: The connection string copy button in the Connection String Only tab of the Atlas UI

Save your connection string to a safe location.

Connect to Your Application
~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. procedure::
:style: connected

.. step:: Create your Node.js Application

Create a file to contain your application called ``index.js`` in your
project directory. Add the following code, assigning the ``uri``
variable the value of your connection string.


.. code-block:: js

const { MongoClient } = require("mongodb");

// Replace the uri string with your connection string.
const uri =
"mongodb+srv://<user>:<password>@<cluster-url>?retryWrites=true&w=majority";

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);

.. tip::

The preceding code example assigns the ``MongoClient`` variable using
:mdn:`object destructuring <Web/JavaScript/Reference/Operators/Destructuring_assignment#object_destructuring>`,
introduced in Node.js v6. You can create an instance of a
``MongoClient`` without using object destructuring as shown in the
following code:

.. code-block:: js

const MongoClient = require("mongodb").MongoClient;

.. step:: Run your Node.js Application

Run the application you created from the previous step from the
command line:

.. code-block:: none

node index.js

You should see the details of the retrieved movie document in the output:

.. code-block:: none

{
_id: ...,
plot: 'A young man is accidentally sent 30 years into the past...',
genres: [ 'Adventure', 'Comedy', 'Sci-Fi' ],
...
title: 'Back to the Future',
...
}
MongoDB deployments. You can create your own free (no credit card
required) MongoDB Atlas deployment by following the steps in this guide.

If you encounter an error or no output, check whether you specified the
proper connection string in the application code, and loaded the sample
data set in your Atlas cluster.
Follow the steps in this guide to connect a sample Node.js application to
a MongoDB Atlas deployment.

At this point, you should have a working application that uses
the {+driver-short+} to connect to your MongoDB instance, runs a query
on the sample data, and prints out the result.
.. button:: Next: Download and Install
:uri: /quick-start/download-and-install/

Next Steps
----------
.. toctree::

Learn how to read and modify data using the {+driver-short+} in our
:ref:`CRUD Operations <node-crud-landing>` guide or how to perform common
operations in our :ref:`Usage Examples <node-usage-examples>`.
/quick-start/download-and-install/
/quick-start/create-a-deployment/
/quick-start/create-a-connection-string/
/quick-start/connect-to-mongodb/
/quick-start/next-steps/
Loading