@@ -13,31 +13,31 @@ Quick Start
13
13
:class: singlecol
14
14
15
15
This guide shows you how to create an application that uses the
16
- **Node.js driver** to connect to a ** MongoDB Atlas cluster** . If you
17
- prefer to connect to MongoDB using a different driver or programming
16
+ {+ driver-long+} to connect to a MongoDB cluster hosted on MongoDB Atlas . If
17
+ you prefer to connect to MongoDB using a different driver or programming
18
18
language, see our :driver:`list of official drivers <>`.
19
19
20
- The Node.js driver is an interface through which you can connect to and
21
- communicate with MongoDB instances .
20
+ The {+ driver-short+} is a library you can use to connect to and communicate
21
+ with MongoDB.
22
22
23
- MongoDB Atlas is a fully- managed cloud database service that hosts your data
24
- on MongoDB instances. We show you how to get started with your own free (no
25
- credit card required) instance in this guide.
23
+ MongoDB Atlas is a fully managed cloud database service that hosts your
24
+ MongoDB servers. You can get started with your own free (no credit card
25
+ required) MongoDB instance with this guide.
26
26
27
- Follow the steps below to connect your Node.js application with a MongoDB
28
- instance.
27
+ Follow the steps below to connect a sample Node.js application to a MongoDB
28
+ instance on MongoDB Atlas .
29
29
30
30
Set up Your Project
31
31
-------------------
32
32
33
33
Install Node and npm
34
34
~~~~~~~~~~~~~~~~~~~~
35
35
36
- Ensure your system has Node.js version 12 or later and a compatible
37
- version of npm (Node Package Manager) installed. For information on how
38
- to check your version of Node and npm, as well as installation
39
- instructions, see `downloading and installing Node.js and npm
40
- <https://docs.npmjs.com/downloading-and-installing-node-js-and-npm>`_ .
36
+ Ensure you have Node.js v12 or later and npm (Node Package Manager) installed
37
+ in your development environment.
38
+
39
+ For information on how to install Node.js and npm, see
40
+ `downloading and installing Node.js and npm <https://docs.npmjs.com/downloading-and-installing-node-js-and-npm>`__ .
41
41
42
42
Select the :guilabel:`Clone the Project` tab if you want to build your
43
43
project from a premade dependency file or the :guilabel:`Create the Project`
@@ -48,9 +48,9 @@ tab if you want to create the project dependency file yourself.
48
48
.. tab:: Clone the Project
49
49
:tabid: clone the project
50
50
51
- First, ensure you have Git installed in your development
52
- environment or otherwise install it using `Git's Getting Started Guide
53
- <https://git-scm.com/book/en/v2/Getting-Started-Installing-Git>`__.
51
+ Ensure you have git installed in your development environment.
52
+
53
+ For information on how to install git, see `Git - Getting Started Guide <https://git-scm.com/book/en/v2/Getting-Started-Installing-Git>`__.
54
54
55
55
Then, from your shell, clone the `js-starter repository
56
56
<https://github.com/mongodb-university/js-starter>`__:
@@ -65,7 +65,7 @@ tab if you want to create the project dependency file yourself.
65
65
66
66
cd js-starter
67
67
68
- Then, install the Node.js driver:
68
+ Then, install the {+ driver-short+} :
69
69
70
70
.. code-block:: bash
71
71
@@ -106,7 +106,7 @@ tab if you want to create the project dependency file yourself.
106
106
default values for your project settings. If you want to
107
107
interactively select your project settings, omit the ``-y`` flag.
108
108
109
- Then, install the Node.js driver:
109
+ Then, install the {+ driver-short+} :
110
110
111
111
.. code-block:: bash
112
112
@@ -118,138 +118,128 @@ tab if you want to create the project dependency file yourself.
118
118
- Saves the package in the ``node_modules`` directory
119
119
- Records the dependency information in the ``package.json`` file
120
120
121
- At this point, you are ready to use the MongoDB Node.js driver with your
121
+ At this point, you are ready to use the {+ driver-short+} with your
122
122
application.
123
123
124
- Create a MongoDB Cluster
125
- ------------------------
124
+ .. procedure::
125
+ :style: connected
126
126
127
- Set up a Free Tier Cluster in Atlas
128
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
127
+ .. step:: Create a Free Tier Cluster in Atlas
129
128
130
- After installing the MongoDB Node.js driver, create a MongoDB instance to store
131
- and manage your data. Complete the
132
- :atlas:`Get Started with Atlas </getting-started?tck=docs_driver_nodejs>` guide
133
- to set up a new Atlas account, a free cluster (MongoDB instance), and load
134
- sample data into your cluster.
129
+ Create a free tier MongoDB cluster on MongoDB Atlas to store and manage
130
+ your data. MongoDB Atlas hosts and manages your MongoDB database in the
131
+ cloud. Complete the :atlas:`Get Started with Atlas </getting-started?tck=docs_driver_nodejs>`
132
+ guide to set up a new Atlas account, a free tier cluster (a shared
133
+ MongoDB instance) and load sample data into your cluster.
135
134
136
- After completing the steps in the Atlas guide, you should have a new MongoDB
137
- cluster deployed in Atlas, a database user with access to the cluster, and
138
- sample datasets loaded into the cluster.
135
+ .. step:: Connect to your Cluster
139
136
140
- Connect to your Cluster
141
- -----------------------
137
+ You can connect to your MongoDB cluster by providing a
138
+ **connection string** which instructs the driver on where and how to
139
+ connect. The connection string includes information on the hostname
140
+ or IP address and port of your cluster, the authentication mechanism,
141
+ user credentials when applicable, and other connection options.
142
142
143
- In this step, we create and run an application that uses the MongoDB Node.js
144
- driver to connect to your instance of MongoDB and run a query on the sample
145
- data.
143
+ To connect to an instance or cluster not hosted on Atlas, see
144
+ :ref:`Other Ways to Connect to MongoDB <node-other-ways-to-connect>`.
146
145
147
- We pass instructions to the driver on where and how to connect to your
148
- MongoDB instance in a string called the *connection string*. This string
149
- includes information on the hostname or IP address and port of your
150
- instance, authentication mechanism, user credentials when applicable, and
151
- other connection options.
146
+ To retrieve your connection string for the cluster you created in
147
+ the previous step, log into your Atlas account and navigate to the
148
+ :guilabel:`Database` section and click the :guilabel:`Connect` button
149
+ for the cluster that you want to connect to as shown below.
152
150
153
- If you are connecting to an instance or cluster that is not hosted by Atlas,
154
- see :ref:`Other Ways to Connect to MongoDB <node-other-ways-to-connect>` for
155
- instructions on how to format your connection string.
151
+ .. figure:: /includes/figures/atlas_connection_select_cluster.png
152
+ :alt: The connect button in the clusters section of the Atlas UI
156
153
157
- To retrieve your connection string for the instance and user you created in
158
- the previous step, log into your Atlas account and navigate to the
159
- :guilabel:`Database` section and click the :guilabel:`Connect ` button for the cluster that you
160
- want to connect to as shown below.
154
+ Proceed to the :guilabel:`Connect Your Application` section and select
155
+ the {+driver-short+}. Select the :guilabel:`Connection String Only` tab
156
+ and click the :guilabel:`Copy ` button to copy the connection string to
157
+ your clipboard as shown below.
161
158
162
- .. figure:: /includes/figures/atlas_connection_select_cluster .png
163
- :alt: The connect button in the clusters section of the Atlas UI
159
+ .. figure:: /includes/figures/atlas_connection_copy_string_node .png
160
+ :alt: The connection string copy button in the Connection String Only tab of the Atlas UI
164
161
165
- Proceed to the :guilabel:`Connect Your Application` step and select the Node.js
166
- driver. Select the :guilabel:`Connection String Only` tab and click the
167
- :guilabel:`Copy` button to copy the *connection string* to your clipboard
168
- as shown below.
162
+ Save your connection string to a safe location.
169
163
170
- .. figure:: /includes/figures/atlas_connection_copy_string_node.png
171
- :alt: The connection string copy button in the Connection String Only tab of the Atlas UI
164
+ .. step:: Create your Node.js Application
172
165
173
- Save your connection string to a safe location that you can access in the
174
- next step.
166
+ Create a file to contain your application called ``index.js`` in your
167
+ project directory. Add the following code, assigning the ``uri``
168
+ variable the value of your connection string.
175
169
176
- Next, create a file to contain your application called ``index.js`` in your
177
- ``node_quickstart`` directory. Add the following code, replacing the ``uri``
178
- variable with your connection string. Make sure to replace the "<password>"
179
- section of the connection string with the password you created for your user
180
- that has **atlasAdmin** permissions.
181
170
182
- .. tip::
171
+ .. code-block:: js
183
172
184
- The line of code assigning the ``MongoClient`` variable uses
185
- :mdn:`Object destructuring <Web/JavaScript/Reference/Operators/Destructuring_assignment#object_destructuring>`,
186
- introduced to Node in v6. If you are using an older version of Node,
187
- use the following to declare ``MongoClient``.
173
+ const { MongoClient } = require("mongodb");
188
174
189
- .. code-block:: js
175
+ // Replace the uri string with your connection string.
176
+ const uri =
177
+ "mongodb+srv://<user>:<password>@<cluster-url>?retryWrites=true&writeConcern=majority";
190
178
191
- const MongoClient = require("mongodb").MongoClient ;
179
+ const client = new MongoClient(uri) ;
192
180
193
- .. code-block:: js
181
+ async function run() {
182
+ try {
183
+ const database = client.db('sample_mflix');
184
+ const movies = database.collection('movies');
194
185
195
- const { MongoClient } = require("mongodb");
186
+ // Query for a movie that has the title 'Back to the Future'
187
+ const query = { title: 'Back to the Future' };
188
+ const movie = await movies.findOne(query);
196
189
197
- // Replace the uri string with your MongoDB deployment's connection string.
198
- const uri =
199
- "mongodb+srv://<user>:<password>@<cluster-url>?retryWrites=true&writeConcern=majority";
190
+ console.log(movie);
191
+ } finally {
192
+ // Ensures that the client will close when you finish/error
193
+ await client.close();
194
+ }
195
+ }
196
+ run().catch(console.dir);
200
197
201
- const client = new MongoClient(uri);
198
+ .. tip::
202
199
203
- async function run() {
204
- try {
205
- await client.connect();
200
+ The preceding code example assigns the ``MongoClient`` variable using
201
+ :mdn:`object destructuring <Web/JavaScript/Reference/Operators/Destructuring_assignment#object_destructuring>`,
202
+ introduced in Node.js v6. You can create an instance of a
203
+ ``MongoClient`` without using object destructuring as shown in the
204
+ following code:
206
205
207
- const database = client.db('sample_mflix');
208
- const movies = database.collection('movies');
206
+ .. code-block:: js
209
207
210
- // Query for a movie that has the title 'Back to the Future'
211
- const query = { title: 'Back to the Future' };
212
- const movie = await movies.findOne(query);
208
+ const MongoClient = require("mongodb").MongoClient;
213
209
214
- console.log(movie);
215
- } finally {
216
- // Ensures that the client will close when you finish/error
217
- await client.close();
218
- }
219
- }
220
- run().catch(console.dir);
210
+ .. step:: Run your Node.js Application
221
211
222
- Run the sample code with the following command from your command line:
212
+ Run the application you created from the previous step from the
213
+ command line:
223
214
224
- .. code-block:: none
215
+ .. code-block:: none
225
216
226
- node index.js
217
+ node index.js
227
218
228
- When you run the command, the sample code should output the details of the
229
- movie which resembles the following:
219
+ You should see the details of the retrieved movie document in the output:
230
220
231
- .. code-block:: none
221
+ .. code-block:: none
232
222
233
- {
234
- _id: ...,
235
- plot: 'A young man is accidentally sent 30 years into the past...',
236
- genres: [ 'Adventure', 'Comedy', 'Sci-Fi' ],
237
- ...
238
- title: 'Back to the Future',
239
- ...
240
- }
223
+ {
224
+ _id: ...,
225
+ plot: 'A young man is accidentally sent 30 years into the past...',
226
+ genres: [ 'Adventure', 'Comedy', 'Sci-Fi' ],
227
+ ...
228
+ title: 'Back to the Future',
229
+ ...
230
+ }
241
231
242
- If you receive an error, check whether you included the proper connection
243
- string in the application code, and loaded the sample dataset in your Atlas
244
- cluster.
232
+ If you encounter an error or no output , check whether you specified the
233
+ proper connection string in the application code, and loaded the sample
234
+ data set in your Atlas cluster.
245
235
246
- After completing this step , you should have a working application that uses
247
- the Node.js driver to connect to your MongoDB instance, run a query on the
248
- sample data, and prints out the result.
236
+ At this point , you should have a working application that uses
237
+ the {+ driver-short+} to connect to your MongoDB instance, runs a query
238
+ on the sample data, and prints out the result.
249
239
250
240
Next Steps
251
241
----------
252
242
253
- Learn how to read and modify data using the Node.js driver in our
254
- :doc :`CRUD Operations guide </fundamentals/ crud>` or how to perform common
255
- operations in our :doc:`usage examples </ usage-examples>`.
243
+ Learn how to read and modify data using the {+ driver-short+} in our
244
+ :ref :`CRUD Operations <node- crud-landing>` guide or how to perform common
245
+ operations in our :ref:`Usage Examples <node- usage-examples>`.
0 commit comments