|
| 1 | +.. _mdb-shell-write-scripts: |
| 2 | + |
| 3 | +================================= |
| 4 | +Write Scripts for the |mdb-shell| |
| 5 | +================================= |
| 6 | + |
| 7 | +.. default-domain:: mongodb |
| 8 | + |
| 9 | +.. contents:: On this page |
| 10 | + :local: |
| 11 | + :backlinks: none |
| 12 | + :depth: 2 |
| 13 | + :class: singlecol |
| 14 | + |
| 15 | +You can write scripts for the |mdb-shell| that manipulate data in |
| 16 | +MongoDB or perform administrative operations. |
| 17 | + |
| 18 | +This tutorial introduces using the |mdb-shell| with JavaScript to |
| 19 | +access MongoDB. |
| 20 | + |
| 21 | +.. _mdb-shell-open-new-connections-in-shell: |
| 22 | + |
| 23 | +Open a New Connection |
| 24 | +--------------------- |
| 25 | + |
| 26 | +From the |mdb-shell| or from a JavaScript file, you can instantiate |
| 27 | +database connections using the :method:`Mongo()` method: |
| 28 | + |
| 29 | +.. code-block:: javascript |
| 30 | + :copyable: false |
| 31 | + |
| 32 | + new Mongo() |
| 33 | + new Mongo(<host>) |
| 34 | + new Mongo(<host:port>) |
| 35 | + |
| 36 | +.. note:: |
| 37 | + |
| 38 | + The |mdb-shell| does not support the |
| 39 | + :manual:`ClientSideFieldLevelEncryptionOptions |
| 40 | + </reference/method/Mongo/#clientsidefieldlevelencryptionoptions>` |
| 41 | + document with the :method:`Mongo()` method. |
| 42 | + |
| 43 | +Consider a MongoDB instance running on localhost on the default port. |
| 44 | + |
| 45 | +.. example:: |
| 46 | + |
| 47 | + The following example: |
| 48 | + |
| 49 | + - Instantiates a new connection to the instance, and |
| 50 | + - Sets the global ``db`` variable to ``myDatabase`` using the |
| 51 | + :method:`getDB()` method. |
| 52 | + |
| 53 | + .. code-block:: javascript |
| 54 | + |
| 55 | + conn = Mongo(); |
| 56 | + db = conn.getDB("myDatabase"); |
| 57 | + |
| 58 | +If you connect to a MongoDB instance that enforces access control, you |
| 59 | +must include the credentials in the :manual:`connection string |
| 60 | +</reference/connection-string/>`. |
| 61 | + |
| 62 | +.. example:: |
| 63 | + |
| 64 | + The following command connects to a MongoDB instance that is: |
| 65 | + |
| 66 | + - Running on ``localhost`` on the default port, and |
| 67 | + - Secured using :manual:`SCRAM </core/security-scram/>`. |
| 68 | + |
| 69 | + .. code-block:: javascript |
| 70 | + |
| 71 | + conn = Mongo("mongodb://<username>:<password>@localhost:27017/<authDB>"); |
| 72 | + |
| 73 | +.. the manual page says to use db.auth(), which isn't implemented yet. |
| 74 | + this is the only way I could get it to work. |
| 75 | + https://docs.mongodb.com/manual/tutorial/write-scripts-for-the-mongo-shell/#opening-new-connections |
| 76 | + |
| 77 | +.. include:: /includes/admonitions/note-redact-credentials-command-history.rst |
| 78 | + |
| 79 | +Additionally, you can use the :manual:`connect() |
| 80 | +</reference/method/connect/>` method to connect to the MongoDB instance. |
| 81 | + |
| 82 | +.. example:: |
| 83 | + |
| 84 | + The following command: |
| 85 | + |
| 86 | + - Connects to the MongoDB instance that is running on ``localhost`` with |
| 87 | + the non-default port ``27020``, and |
| 88 | + - Sets the global ``db`` variable. |
| 89 | + |
| 90 | + .. code-block:: javascript |
| 91 | + |
| 92 | + db = connect("localhost:27020/myDatabase"); |
| 93 | + |
| 94 | +.. skipping the Differences Between Interactive and Scripted mongo |
| 95 | + section as most of the javascript equivalents to the shell helpers |
| 96 | + have not yet been implemented in mongosh -- revisit later |
| 97 | + https://docs.mongodb.com/manual/tutorial/write-scripts-for-the-mongo-shell/#differences-between-interactive-and-scripted-mongo |
| 98 | + |
| 99 | +.. also skippping --eval section as I'm having issues getting it to work |
| 100 | + https://docs.mongodb.com/manual/tutorial/write-scripts-for-the-mongo-shell/#eval-option |
| 101 | + |
| 102 | +Use ``require()`` to Include External Files and Modules |
| 103 | +------------------------------------------------------- |
| 104 | + |
| 105 | +.. important:: |
| 106 | + |
| 107 | + A complete description of Node.js, modules, and the |
| 108 | + `require() <https://nodejs.org/api/modules.html#modules_require_id>`__ |
| 109 | + function is out of scope for this tutorial. To learn more, refer to |
| 110 | + the `Node.js Documentation <https://nodejs.org/api/modules.html>`__. |
| 111 | + |
| 112 | +You can use the `require() |
| 113 | +<https://nodejs.org/api/modules.html#modules_require_id>`__ function in |
| 114 | +the |mdb-shell| to include modules which exist in separate files. |
| 115 | + |
| 116 | +Require a File |
| 117 | +~~~~~~~~~~~~~~ |
| 118 | + |
| 119 | +You can ``require()`` JavaScript files in the |mdb-shell| without any |
| 120 | +additional setup or configuration. |
| 121 | + |
| 122 | +.. note:: |
| 123 | + |
| 124 | + The |mdb-shell| does not execute files imported with ``require()``. |
| 125 | + The |mdb-shell| adds everything from an imported file to the current |
| 126 | + execution scope. |
| 127 | + |
| 128 | +.. example:: |
| 129 | + |
| 130 | + Use one of the following commands include a file from the current |
| 131 | + working directory named ``tests.js``: |
| 132 | + |
| 133 | + .. code-block:: javascript |
| 134 | + |
| 135 | + require('./tests.js') |
| 136 | + |
| 137 | + .. code-block:: javascript |
| 138 | + |
| 139 | + var tests = require('./tests.js') |
| 140 | + |
| 141 | +Require a Native Module |
| 142 | +~~~~~~~~~~~~~~~~~~~~~~~ |
| 143 | + |
| 144 | +You can ``require()`` native Node modules (such as |
| 145 | +`fs <https://nodejs.org/api/fs.html#fs_file_system>`__) in the |
| 146 | +|mdb-shell| without any additional setup or configuration. |
| 147 | + |
| 148 | +.. example:: |
| 149 | + |
| 150 | + Use one of the following commands to include the ``fs`` module: |
| 151 | + |
| 152 | + .. code-block:: javascript |
| 153 | + |
| 154 | + require('fs') |
| 155 | + |
| 156 | + .. code-block:: javascript |
| 157 | + |
| 158 | + var fs = require('fs'); |
| 159 | + |
| 160 | +Require a Non-Native Module |
| 161 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 162 | + |
| 163 | +To ``require()`` non-native Node modules (such as those downloaded from |
| 164 | +`npm <https://www.npmjs.com/>`__) you must install the module globally |
| 165 | +or to the ``node_modules`` directory in your current working directory. |
| 166 | + |
| 167 | +Once you install or copy your desired package to one of the module |
| 168 | +directories, you can ``require()`` that package. |
| 169 | + |
| 170 | +.. example:: |
| 171 | + |
| 172 | + Use one of the following commands to include the |
| 173 | + `moment <https://www.npmjs.com/package/moment>`__: |
| 174 | + |
| 175 | + .. code-block:: javascript |
| 176 | + |
| 177 | + require('moment') |
| 178 | + |
| 179 | + .. code-block:: javascript |
| 180 | + |
| 181 | + var moment = require('moment') |
| 182 | + |
| 183 | +.. _mdb-shell-execute-file: |
| 184 | + |
| 185 | +Execute a JavaScript File |
| 186 | +------------------------- |
| 187 | + |
| 188 | +You can execute a ``.js`` file from within the |mdb-shell| |
| 189 | +using the ``.load`` command. |
| 190 | + |
| 191 | +.. example:: |
| 192 | + |
| 193 | + The following command loads and executes the ``myjstest.js`` file: |
| 194 | + |
| 195 | + .. code-block:: javascript |
| 196 | + |
| 197 | + .load myjstest.js |
| 198 | + |
| 199 | +The ``.load`` command accepts relative and absolute paths. If the |
| 200 | +current working directory of the |mdb-shell| is ``/data/db``, |
| 201 | +and ``myjstest.js`` resides in the ``/data/db/scripts`` directory, then |
| 202 | +the following calls within the |mdb-shell| are equivalent: |
| 203 | + |
| 204 | +.. code-block:: javascript |
| 205 | + :copyable: false |
| 206 | + |
| 207 | + .load scripts/myjstest.js |
| 208 | + .load /data/db/scripts/myjstest.js |
| 209 | + |
| 210 | +.. note:: |
| 211 | + |
| 212 | + There is no search path for the ``.load`` command. If the desired |
| 213 | + script is not in the current working directory or the full specified |
| 214 | + path, the |mdb-shell| cannot access the file. |
0 commit comments