Skip to content

Commit f057605

Browse files
Merge pull request #156 from davemungo/DOCSP-19198-command-line-file-param-causes-error
DOCSP-16198 command line file name parameter
2 parents ec3be7f + 04ed64d commit f057605

File tree

1 file changed

+156
-4
lines changed

1 file changed

+156
-4
lines changed

source/write-scripts.txt

Lines changed: 156 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,14 @@ The following command:
107107
Execute a JavaScript File
108108
-------------------------
109109

110+
Execute a Script from Within the mongosh Console
111+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
112+
110113
You can execute a ``.js`` file from within the |mdb-shell|
111114
using the :method:`load()` method.
112115

116+
.. _mongosh-ex-run-script:
117+
113118
.. example::
114119

115120
The following example creates and executes a script that:
@@ -123,7 +128,7 @@ using the :method:`load()` method.
123128

124129
.. code-block:: javascript
125130

126-
db = connect('mongodb://localhost/myDatabase');
131+
db = connect( 'mongodb://localhost/myDatabase' );
127132

128133
db.movies.insertMany( [
129134
{
@@ -179,7 +184,7 @@ using the :method:`load()` method.
179184

180185
.. code-block:: javascript
181186

182-
load("connect-and-insert.js")
187+
load( "connect-and-insert.js" )
183188

184189
#. To confirm that the documents loaded correctly, use the
185190
``myDatabase`` collection and query the ``movies`` collection.
@@ -197,15 +202,162 @@ the following calls within the |mdb-shell| are equivalent:
197202
.. code-block:: javascript
198203
:copyable: false
199204

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" )
202207

203208
.. note::
204209

205210
There is no search path for the :method:`load()` method. If the target
206211
script is not in the current working directory or the full specified
207212
path, the |mdb-shell| cannot access the file.
208213

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+
209361
.. toctree::
210362
:titlesonly:
211363

0 commit comments

Comments
 (0)