1
1
.. _node-get-started:
2
2
3
- ============================
4
- Getting Started with Node.js
5
- ============================
3
+ =======================================
4
+ Getting Started with the Node.js Driver
5
+ =======================================
6
6
7
7
.. facet::
8
8
:name: genre
@@ -22,19 +22,19 @@ Overview
22
22
--------
23
23
24
24
This guide shows you how to create an application that uses the
25
- {+driver-long+} to connect to a MongoDB cluster hosted on MongoDB Atlas. If
26
- you prefer to connect to MongoDB using a different driver or programming
27
- language, see our :driver:`list of official drivers <>`.
28
-
25
+ {+driver-long+} to connect to a MongoDB cluster hosted on MongoDB Atlas.
29
26
The {+driver-short+} is a library of functions that you can use to connect
30
27
to and communicate with MongoDB.
31
28
32
- MongoDB Atlas is a fully managed cloud database service that hosts your
33
- MongoDB deployments. You can create your own free (no credit card
34
- required) MongoDB Atlas deployment by following the steps in this guide.
29
+ .. tip::
30
+
31
+ MongoDB Atlas is a fully managed cloud database service that hosts your
32
+ MongoDB deployments. You can create your own free (no credit card
33
+ required) MongoDB Atlas deployment by following the steps in this guide.
35
34
36
35
Follow the steps in this guide to connect a sample Node.js application to
37
- a MongoDB Atlas deployment.
36
+ a MongoDB Atlas deployment. If you prefer to connect to MongoDB using a different
37
+ driver or programming language, see our :driver:`list of official drivers <>`.
38
38
39
39
.. _node-quick-start-download-and-install:
40
40
.. _node-get-started-download-and-install:
@@ -45,15 +45,19 @@ Download and Install
45
45
.. procedure::
46
46
:style: connected
47
47
48
- .. step:: Install Node and npm
48
+ .. step:: Install dependencies
49
49
50
- Ensure you have Node.js {+min-node-version+} or later and
51
- npm (Node Package Manager) installed in your development environment.
50
+ Ensure you have the following dependencies installed in
51
+ your development environment:
52
+
53
+ - Node.js {+min-node-version+} or later
54
+ - npm (Node Package Manager)
52
55
53
- For information on how to install Node.js and npm, see
54
- `downloading and installing Node.js and npm <https://docs.npmjs.com/downloading-and-installing-node-js-and-npm>`__.
56
+ To learn how to install Node.js and npm, see
57
+ `Downloading and installing Node.js and npm <https://docs.npmjs.com/downloading-and-installing-node-js-and-npm>`__
58
+ in the npm documentation.
55
59
56
- .. step:: Create a Project Directory
60
+ .. step:: Create a project directory
57
61
58
62
In your shell, run the following command to create a
59
63
directory called ``node_quickstart`` for this project:
@@ -62,27 +66,21 @@ Download and Install
62
66
63
67
mkdir node_quickstart
64
68
65
- Run the following command to navigate into the project
66
- directory:
69
+ Then, run the following commands to navigate into the
70
+ directory and initialize your Node.js project :
67
71
68
72
.. code-block:: bash
69
73
70
74
cd node_quickstart
71
-
72
- Run the following command to initialize your Node.js project:
73
-
74
- .. code-block:: bash
75
-
76
75
npm init -y
77
-
78
- When this command successfully completes, you have a ``package.json``
76
+
77
+ When the initialization command successfully completes, you have a ``package.json``
79
78
file in your ``node_quickstart`` directory.
80
79
81
-
82
80
.. step:: Install the Node.js Driver
83
81
84
- Run the following command in your shell to install
85
- the driver in your project directory :
82
+ Run the following command from your project directory to install
83
+ the driver:
86
84
87
85
.. code-block:: bash
88
86
@@ -94,10 +92,8 @@ Download and Install
94
92
- Saves the package in the ``node_modules`` directory
95
93
- Records the dependency information in the ``package.json`` file
96
94
97
- After you complete these steps, you have Node.js and npm installed
98
- and a new project directory with the driver dependencies installed.
99
-
100
- .. include:: /includes/quick-start/troubleshoot.rst
95
+ After you complete these steps, you have a new project directory with
96
+ the driver dependencies installed.
101
97
102
98
.. _node-quick-start-create-deployment:
103
99
.. _node-get-started-create-deployment:
@@ -112,13 +108,13 @@ your MongoDB database in the cloud.
112
108
.. procedure::
113
109
:style: connected
114
110
115
- .. step:: Create a Free MongoDB deployment on Atlas
111
+ .. step:: Create a free MongoDB deployment on Atlas
116
112
117
113
Complete the :atlas:`Get Started with Atlas </getting-started?tck=docs_driver_nodejs>`
118
114
guide to set up a new Atlas account and load sample data into a new free
119
115
tier MongoDB deployment.
120
116
121
- .. step:: Save your Credentials
117
+ .. step:: Save your credentials
122
118
123
119
After you create your database user, save that user's
124
120
username and password to a safe location for use in an upcoming step.
@@ -127,7 +123,53 @@ After you complete these steps, you have a new free tier MongoDB
127
123
deployment on Atlas, database user credentials, and sample data loaded
128
124
in your database.
129
125
130
- .. include:: /includes/quick-start/troubleshoot.rst
126
+ .. _node-quick-start-connection-string:
127
+ .. _node-get-started-connection-string:
128
+
129
+ Create a Connection String
130
+ --------------------------
131
+
132
+ You can connect to your MongoDB deployment by providing a
133
+ **connection URI**, also called a *connection string*, which
134
+ instructs the driver on how to connect to a MongoDB deployment
135
+ and how to behave while connected.
136
+
137
+ The connection string includes the hostname or IP address and
138
+ port of your deployment, the authentication mechanism, user credentials
139
+ when applicable, and connection options.
140
+
141
+ .. procedure::
142
+ :style: connected
143
+
144
+ .. step:: Find your MongoDB Atlas connection string
145
+
146
+ To retrieve your connection string for the deployment that
147
+ you created in the :ref:`previous section <node-get-started-create-deployment>`,
148
+ log into your Atlas account and navigate to the
149
+ :guilabel:`Clusters` section and click the :guilabel:`Connect` button
150
+ for your new deployment.
151
+
152
+ .. figure:: /includes/figures/atlas_connection_connect_cluster.png
153
+ :alt: The connect button in the clusters section of the Atlas UI
154
+
155
+ .. step:: Copy your connection string
156
+
157
+ Click the button on the right of the connection string to copy it to
158
+ your clipboard, as shown in the following screenshot:
159
+
160
+ .. figure:: /includes/figures/atlas_connection_copy_string_node.png
161
+ :alt: The connection string copy button in the Atlas UI
162
+
163
+ .. step:: Update the placeholders
164
+
165
+ Paste your connection string into a file in your preferred text editor
166
+ and replace the ``username`` and ``<db_password>`` placeholders with your
167
+ database user's username and password.
168
+
169
+ Save this file to a safe location for use in the next section.
170
+
171
+ After completing these steps, you have a connection string that
172
+ contains your database username, database password, and database name.
131
173
132
174
.. _node-quick-start-connect-to-mongodb:
133
175
.. _node-get-started-connect-to-mongodb:
@@ -138,10 +180,10 @@ Connect to MongoDB
138
180
.. procedure::
139
181
:style: connected
140
182
141
- .. step:: Create your Node.js Application
183
+ .. step:: Create your Node.js application
142
184
143
- Create a file to contain your application called ``index.js `` in your
144
- ``node_quickstart `` project directory.
185
+ In your ``node_quickstart `` directory, create a file called
186
+ ``index.js `` to contain your application.
145
187
146
188
Copy and paste the following code into the ``index.js`` file:
147
189
@@ -171,21 +213,22 @@ Connect to MongoDB
171
213
}
172
214
run().catch(console.dir);
173
215
174
- .. step:: Assign the Connection String
216
+ .. step:: Assign the connection string
175
217
176
218
Replace the ``<connection string uri>`` placeholder with the
177
219
connection string that you copied from the :ref:`node-quick-start-connection-string`
178
220
step of this guide.
179
221
180
- .. step:: Run your Node.js Application
222
+ .. step:: Run your Node.js application
181
223
182
- In your shell, run the following command to start this application:
224
+ From your project directory, run the following command to start
225
+ the application:
183
226
184
227
.. code-block:: none
185
228
186
229
node index.js
187
230
188
- The output includes details of the retrieved movie document:
231
+ The output includes details about the retrieved movie document:
189
232
190
233
.. code-block:: none
191
234
@@ -198,15 +241,13 @@ Connect to MongoDB
198
241
...
199
242
}
200
243
201
- If you encounter an error or see no output, check whether you specified the
202
- proper connection string in the ``index.js`` file, and that you loaded the
244
+ If you encounter an error or see no output, verify that you specified the
245
+ proper connection string in the ``index.js`` file and that you loaded the
203
246
sample data.
204
247
205
248
After you complete these steps, you have a working application that
206
- uses the driver to connect to your MongoDB deployment, runs a query on
207
- the sample data, and prints out the result.
208
-
209
- .. include:: /includes/quick-start/troubleshoot.rst
249
+ uses the driver to connect to your MongoDB deployment, run a query on
250
+ the sample data, and print out the result.
210
251
211
252
.. _node-quick-start-next-steps:
212
253
.. _node-get-started-next-steps:
@@ -216,11 +257,13 @@ Next Steps
216
257
217
258
Congratulations on completing the quick start tutorial!
218
259
260
+ .. include:: /includes/quick-start/troubleshoot.rst
261
+
219
262
In this tutorial, you created a Node.js application that
220
263
connects to a MongoDB deployment hosted on MongoDB Atlas
221
264
and retrieves a document that matches a query.
222
265
223
- Learn more about the {+driver-long +} from the following resources:
266
+ Learn more about the {+driver-short +} from the following resources:
224
267
225
268
- Discover how to perform read and write operations in the
226
269
:ref:`CRUD Operations <node-crud-landing>` section.
0 commit comments