Skip to content
Closed
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
24 changes: 24 additions & 0 deletions source/includes/fact-execute-javascript-from-shell.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
You can execute a ``.js`` file from within the :program:`mongo` shell,
using the :method:`load()` function. Consider the following example:

.. code-block:: javascript

load("myjstest.js")

This method loads and executes the :filename:`myjstest.js` file.

The :method:`load()` method accepts relative and absolute paths.
If the current working directory of the :program:`mongo` shell
is :file:`/data/db`, and the :file:`myjstest.js` resides in the
:file:`/data/db/scripts` directory, then the following calls within
the :program:`mongo` shell would be equivalent:

.. code-block:: javascript

load("scripts/myjstest.js")
load("/data/db/scripts/myjstest.js")

.. note:: There is no search path for the :method:`load()` command. If
the desired script is not in the specified directory, the shell will
be unable to access the file.

11 changes: 11 additions & 0 deletions source/reference/method/load-param.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
object:
name: load()
type: method
field:
optional: false
type: param
name: filename
type: string
position: 2
description: "Specifies the path of a JavaScript file to execute."
...
32 changes: 17 additions & 15 deletions source/reference/method/load.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@ load()

.. method:: load( <file> )

:param string file: Specify a path and file name containing
JavaScript.
.. include:: /reference/method/load-param.rst

This native function loads and runs a JavaScript file into the
current shell environment. To run JavaScript with the mongo shell,
you can either:
:method:`load()` is a native function that that evaluates a
JavaScript file in the current :program:`mongo` shell environment.

- use the ":option:`--eval <mongo --eval>`" option when invoking
the shell to evaluate a small amount of JavaScript code, or
Specify filenames with relative or absolute paths. When using
relative path names, confirm the current directory using the
:method:`pwd()` method.

After executing a file with :method:`path()`, any functions and
variables defined in the file may be referenced from the command
line.

Examples of the :method:`load()` method:

.. code-block:: javascript

load("scripts/myjstest.js")
load("/data/db/scripts/myjstest.js")

- specify a file name with ":ref:`mongo <mongo-shell-file>`".
:program:`mongo` will execute the script and then exit. Add the
:option:`--shell <mongo --shell>` option to return to the shell after
running the command.

Specify files loaded with the :method:`load()` function in relative terms
to the current directory of the :program:`mongo` shell
session. Check the current directory using the ":method:`pwd()`"
function.
6 changes: 6 additions & 0 deletions source/reference/mongo-shell.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ displays some common help methods and commands:
- .. versionadded:: 2.4
Print a list of all available databases.

* - ``load()``

- Execute a JavaScript file. See
:doc:`/tutorial/getting-started-with-the-mongo-shell`
for more information.

Basic Shell JavaScript Operations
----------------------------------

Expand Down
14 changes: 10 additions & 4 deletions source/tutorial/getting-started-with-the-mongo-shell.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ To start the :program:`mongo` shell and connect to your :doc:`MongoDB

db

The command should return ``test``, which is the default database.
To switch databases, issue the ``use <db>`` command, as in the
The operation should return ``test``, which is the default database.
To switch databases, issue the ``use <db>`` helper, as in the
following example:

.. code-block:: javascript

use <database>

To list the available databases, use the command ``show dbs``. See
To list the available databases, use the helper ``show dbs``. See
also :ref:`mongo-shell-getSiblingDB` to access a different database
from the current database without switching your current database
context (i.e. ``db.``.)
Expand Down Expand Up @@ -155,6 +155,11 @@ In addition, you can use the following explicit print methods in the
- ``printjson()`` to print with :term:`JSON` formatting and equivalent
to ``print(tojson(<obj>))``

Evaluate a JavaScript File
--------------------------

.. include:: /includes/fact-execute-javascript-from-shell.rst

Use a Custom Prompt
-------------------

Expand All @@ -166,7 +171,7 @@ prompt. Consider the following examples:

.. example::

Create a prompt with the number of commands issued in the current
Create a prompt with the number of operations issued in the current
session, define the following variables:

.. code-block:: javascript
Expand Down Expand Up @@ -276,3 +281,4 @@ Exit the Shell
--------------

To exit the shell, type ``quit()`` or use the ``<Ctrl-c>`` shortcut.

11 changes: 7 additions & 4 deletions source/tutorial/write-scripts-for-the-mongo-shell.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,25 @@ This returns the output of :method:`db.getCollectionNames()` using the

.. _mongo-shell-javascript-file:

Evaluate a JavaScript file
~~~~~~~~~~~~~~~~~~~~~~~~~~
Execute a JavaScript file
~~~~~~~~~~~~~~~~~~~~~~~~~

You can specify a ``.js`` file to the :program:`mongo` shell, and
:program:`mongo` will evaluate the javascript directly. Consider the
:program:`mongo` will execute the JavaScript directly. Consider the
following example:

.. code-block:: sh

mongo localhost:27017/test myjsfile.js

This operation evaluates the ``myjsfile.js`` script in a
This operation executes the ``myjsfile.js`` script in a
:program:`mongo` shell that connects to the ``test`` :term:`database`
on the :program:`mongod` instance accessible via the ``localhost``
interface on port ``27017``.

Alternately, you can specify the mongodb connection parameters inside
of the javascript file using the ``Mongo()`` constructor. See
:ref:`mongo-shell-new-connections` for more information.

.. include:: /includes/fact-execute-javascript-from-shell.rst