@@ -107,9 +107,14 @@ The following command:
107
107
Execute a JavaScript File
108
108
-------------------------
109
109
110
+ Execute a Script from Within the mongosh Console
111
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
112
+
110
113
You can execute a ``.js`` file from within the |mdb-shell|
111
114
using the :method:`load()` method.
112
115
116
+ .. _mongosh-ex-run-script:
117
+
113
118
.. example::
114
119
115
120
The following example creates and executes a script that:
@@ -123,7 +128,7 @@ using the :method:`load()` method.
123
128
124
129
.. code-block:: javascript
125
130
126
- db = connect('mongodb://localhost/myDatabase');
131
+ db = connect( 'mongodb://localhost/myDatabase' );
127
132
128
133
db.movies.insertMany( [
129
134
{
@@ -179,7 +184,7 @@ using the :method:`load()` method.
179
184
180
185
.. code-block:: javascript
181
186
182
- load("connect-and-insert.js")
187
+ load( "connect-and-insert.js" )
183
188
184
189
#. To confirm that the documents loaded correctly, use the
185
190
``myDatabase`` collection and query the ``movies`` collection.
@@ -197,15 +202,162 @@ the following calls within the |mdb-shell| are equivalent:
197
202
.. code-block:: javascript
198
203
:copyable: false
199
204
200
- load("scripts/connect-and-insert.js")
201
- load("/data/db/scripts/connect-and-insert.js")
205
+ load( "scripts/connect-and-insert.js" )
206
+ load( "/data/db/scripts/connect-and-insert.js" )
202
207
203
208
.. note::
204
209
205
210
There is no search path for the :method:`load()` method. If the target
206
211
script is not in the current working directory or the full specified
207
212
path, the |mdb-shell| cannot access the file.
208
213
214
+ Execute a Script From the Command Line
215
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
216
+
217
+ You can use :binary:`~bin.mongosh` to execute a script from the command
218
+ line without entering the :binary:`~bin.mongosh` console.
219
+
220
+ Use the ``--file`` or ``-f`` parameters to specify the filename.
221
+
222
+ .. code-block::
223
+
224
+ mongosh --port 27500 --file addMovies.js
225
+
226
+ .. tip::
227
+
228
+ If you pass a filename to :binary:`~bin.mongosh` without using the
229
+ parameter flags the connection may fail if there are other command
230
+ line arguments.
231
+
232
+ To pass filenames always use ``--file`` or ``-f``.
233
+
234
+ You may also need to specify connection information in addition to the
235
+ ``--file`` or ``-f`` parameters.
236
+
237
+ .. example::
238
+
239
+ The following example creates scripts and runs them from the command
240
+ line.
241
+
242
+ - ``loadMovies.js``, uses :method:`~db.collection.insertMany()` to a
243
+ update a local MongodDB instance.
244
+ - ``queryMovies.js`` uses :method:`db.collection.find()` to verify
245
+ the update.
246
+
247
+ #. Copy this script and save it as ``loadMovies.js``.
248
+
249
+ .. code-block:: javascript
250
+ :emphasize-lines: 1
251
+
252
+ db = connect( 'mongodb://localhost/films' );
253
+
254
+ db.movies.insertMany( [
255
+ {
256
+ title: 'Titanic',
257
+ year: 1997,
258
+ genres: [ 'Drama', 'Romance' ]
259
+ },
260
+ {
261
+ title: 'Spirited Away',
262
+ year: 2001,
263
+ genres: [ 'Animation', 'Adventure', 'Family' ]
264
+ },
265
+ {
266
+ title: 'Casablanca',
267
+ genres: [ 'Drama', 'Romance', 'War' ]
268
+ }
269
+ ] )
270
+
271
+ .. tip::
272
+
273
+ Verify the connection string in the highlighted line. If your
274
+ MongoDB instance is not running on ``localhost:27017``, you
275
+ must edit the connection string.
276
+
277
+ .. code-block:: javascript
278
+ :emphasize-lines: 1
279
+
280
+ db = connect( 'mongodb://localhost:27500/films' );
281
+
282
+ This connection string is for the:
283
+
284
+ - films database
285
+ - located on localhost
286
+ - listening on port 27500
287
+
288
+ #. Copy this script and save it as ``queryMovies.js``.
289
+
290
+ .. code-block:: javascript
291
+
292
+ db = connect( 'mongodb://localhost/films' );
293
+ printjson( db.movies.find( {} ) );
294
+
295
+ #. Run the scripts from the command line.
296
+
297
+ .. code-block:: javascript
298
+
299
+ mongosh --file loadMovies.js -f queryMovies.js
300
+
301
+ #. Verify the output.
302
+
303
+ .. code-block:: javascript
304
+ :emphasize-lines: 1-2
305
+
306
+ Loading file: loadMovies.js
307
+ Loading file: queryMovies.js
308
+ [
309
+ {
310
+ _id: ObjectId("616f1b8092dbee425b661117"),
311
+ title: 'Titanic',
312
+ year: 1997,
313
+ genres: [ 'Drama', 'Romance' ]
314
+ },
315
+ {
316
+ _id: ObjectId("616f1b8092dbee425b661118"),
317
+ title: 'Spirited Away',
318
+ year: 2001,
319
+ genres: [ 'Animation', 'Adventure', 'Family' ]
320
+ },
321
+ {
322
+ _id: ObjectId("616f1b8092dbee425b661119"),
323
+ title: 'Casablanca',
324
+ genres: [ 'Drama', 'Romance', 'War' ]
325
+ }
326
+ ]
327
+
328
+ The output of the :method:`db.collection.find()` command shows
329
+ that the ``movies`` collection was updated.
330
+
331
+ .. tip::
332
+
333
+ To make the output visible, use ``printjson()`` to call
334
+ :method:`db.collection.find()`.
335
+
336
+ .. code-block:: javascript
337
+
338
+ printjson( db.movies.find( {} ) ) ;
339
+
340
+
341
+ Execute a Script From the Command Line with Authentication
342
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
343
+
344
+ To execute a script against a remote :binary:`mongod` instance that
345
+ requires authentication, specify the connection and authentication
346
+ details in addition to the filename.
347
+
348
+ The following lines are equivalent.
349
+
350
+ .. code-block:: javascript
351
+
352
+ mongosh --host 172.17.0.3 --port 27500 --username filmFan --password superSecret --file loadMovies.js
353
+ mongosh --host 172.17.0.3 --port 27500 -u filmFan -p superSecret -f loadMovies.js
354
+
355
+ .. tip::
356
+
357
+ In shells like ``bash`` and ``zsh``, if you begin a command with a
358
+ space it will not be saved in your command history. This minimizes
359
+ exposure if you enter passwords on the command line.
360
+
209
361
.. toctree::
210
362
:titlesonly:
211
363
0 commit comments