Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: v2.4
Fetching contributors…

Cannot retrieve contributors at this time

136 lines (93 sloc) 4.341 kb
=====================================
Write Scripts for the ``mongo`` Shell
=====================================
.. default-domain:: mongodb
You can write scripts for the :program:`mongo` shell in JavaScript
that manipulate data in MongoDB or perform administrative
operation. For more information about the :program:`mongo` shell see
:doc:`/administration/scripting`, and see the :ref:`running-js-scripts-in-mongo-on-mongod-host`
section for more information about using these :program:`mongo`
script.
This tutorial provides an introduction to writing JavaScript that uses
the :program:`mongo` shell to access MongoDB.
.. _mongo-shell-new-connections:
Opening New Connections
-----------------------
From the :program:`mongo` shell or from a JavaScript file, you can
instantiate database connections using the :method:`Mongo()`
constructor:
.. code-block:: javascript
new Mongo()
new Mongo(<host>)
new Mongo(<host:port>)
Consider the following example that instantiates a new connection to
the MongoDB instance running on localhost on the default port and sets
the global ``db`` variable to ``myDatabase`` using the
:method:`~Mongo.getDB()` method:
.. code-block:: javascript
conn = new Mongo();
db = conn.getDB("myDatabase");
Additionally, you can use the :method:`connect()` method
to connect to the MongoDB instance. The following example connects to
the MongoDB instance that is running on ``localhost`` with the
non-default port ``27020`` and set the global ``db`` variable:
.. code-block:: javascript
db = connect("localhost:27020/myDatabase");
Differences Between Interactive and Scripted ``mongo``
------------------------------------------------------
When writing scripts for the :program:`mongo` shell, consider the
following:
- To set the ``db`` global variable, use the :method:`~Mongo.getDB()`
method or the :method:`connect()` method. You can assign the database
reference to a variable other than ``db``.
- Inside the script, call :method:`db.getLastError()` explicitly to
wait for the result of :doc:`write operations
</core/write-operations>`.
- You **cannot** use any shell helper (e.g. ``use <dbname>``, ``show
dbs``, etc.) inside the JavaScript file because they are not valid
JavaScript.
The following table maps the most common :program:`mongo` shell
helpers to their JavaScript equivalents.
.. include:: /includes/table/helpers-to-javascript.rst
- In interactive mode, :program:`mongo` prints the results of
operations including the content of all cursors. In scripts, either
use the JavaScript ``print()`` function or the :program:`mongo`
specific ``printjson()`` function which returns formatted JSON.
.. example::
To print all items in a result cursor in :program:`mongo` shell
scripts, use the following idiom:
.. code-block:: javascript
cursor = db.collection.find();
while ( cursor.hasNext() ) {
printjson( cursor.next() );
}
.. _mongo-shell-scripting:
Scripting
---------
From the system prompt, use :program:`mongo` to evaluate JavaScript.
``--eval`` option
~~~~~~~~~~~~~~~~~
Use the :option:`--eval <mongo --eval>` option to :program:`mongo` to
pass the shell a JavaScript fragment, as in the following:
.. code-block:: sh
mongo test --eval "printjson(db.getCollectionNames())"
This returns the output of :method:`db.getCollectionNames()` using the
:program:`mongo` shell connected to the :program:`mongod` or
:program:`mongos` instance running on port ``27017`` on the
``localhost`` interface.
.. _mongo-shell-javascript-file:
Execute a JavaScript file
~~~~~~~~~~~~~~~~~~~~~~~~~
You can specify a ``.js`` file to the :program:`mongo` shell, and
:program:`mongo` will execute the JavaScript directly. Consider the
following example:
.. code-block:: sh
mongo localhost:27017/test myjsfile.js
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
Jump to Line
Something went wrong with that request. Please try again.