|
| 1 | +.. _configure-settings-api: |
| 2 | + |
| 3 | +============================================== |
| 4 | +Configure :binary:`~bin.mongosh` Using the API |
| 5 | +============================================== |
| 6 | + |
| 7 | +.. default-domain:: mongodb |
| 8 | + |
| 9 | +.. contents:: On this page |
| 10 | + :local: |
| 11 | + :backlinks: none |
| 12 | + :depth: 1 |
| 13 | + |
| 14 | +The ``config`` API provides methods to examine and update the |
| 15 | +:binary:`~bin.mongosh` configuration. Updates made using the ``config`` |
| 16 | +API persist between sessions. |
| 17 | + |
| 18 | +.. note:: |
| 19 | + |
| 20 | + The ``config`` API can be used within the :binary:`~bin.mongosh` |
| 21 | + command line interface. It has no effect in the |
| 22 | + :compass:`embedded Compass shell </embedded-shell>`. |
| 23 | + |
| 24 | +Syntax |
| 25 | +------ |
| 26 | + |
| 27 | +Print the current :binary:`~bin.mongosh` configuration: |
| 28 | + |
| 29 | +.. _mongosh-config: |
| 30 | + |
| 31 | +.. code-block:: javascript |
| 32 | + |
| 33 | + config |
| 34 | + |
| 35 | +Return the current value for ``<property>``: |
| 36 | + |
| 37 | +.. code-block:: javascript |
| 38 | + |
| 39 | + config.get( "<property>" ) |
| 40 | + |
| 41 | +.. _mongosh-config-set: |
| 42 | + |
| 43 | +Change the current setting of ``<property>`` to ``<value>``: |
| 44 | + |
| 45 | +.. code-block:: javascript |
| 46 | + |
| 47 | + config.set( "<property>", <value> ) |
| 48 | + |
| 49 | +Reset a ``<property>`` to the default value: |
| 50 | + |
| 51 | +.. code-block:: javascript |
| 52 | + |
| 53 | + config.reset( "<property>" ) |
| 54 | + |
| 55 | +Supported ``property`` parameters |
| 56 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 57 | + |
| 58 | +.. include:: /includes/list-table-shell-properties.rst |
| 59 | + |
| 60 | +Behavior |
| 61 | +-------- |
| 62 | + |
| 63 | +Remove or Redact Sensitive Information From History |
| 64 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 65 | + |
| 66 | +:binary:`~bin.mongosh` makes "best-effort" attempts to match patterns |
| 67 | +that normally correspond to certain kinds of sensitive information. |
| 68 | + |
| 69 | +There are patterns that match: |
| 70 | + |
| 71 | +- Certificates and keys |
| 72 | +- Email addresses |
| 73 | +- Generic user directories |
| 74 | +- HTTP(s) URLs |
| 75 | +- IP addresses |
| 76 | +- MongoDB connection strings |
| 77 | + |
| 78 | +Certain operations, such as :method:`connect()`, are considered |
| 79 | +inherently sensitive. If ``redactHistory`` is set to ``remove`` or |
| 80 | +``remove-redact``, lines with these operations will be removed from the |
| 81 | +:ref:`command line history <mdb-shell-command-history>`. |
| 82 | + |
| 83 | +Other operations, like :method:`~db.collection.find()`, sometimes have |
| 84 | +sensitive information like email addresses. The |
| 85 | +:ref:`shell history <mdb-shell-command-history>` will retain these |
| 86 | +lines as entered unless ``redactHistory`` is set to ``remove-redact``. |
| 87 | + |
| 88 | +Behavior with Configuration File |
| 89 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 90 | + |
| 91 | +Settings specified with the ``config`` API: |
| 92 | + |
| 93 | +- Override settings specified in the :ref:`configuration file |
| 94 | + <configure-settings-global>`. |
| 95 | + |
| 96 | +- Persist across restarts. |
| 97 | + |
| 98 | +.. example:: |
| 99 | + |
| 100 | + Consider the following configuration file that sets the |
| 101 | + ``inspectDepth`` setting to ``20``: |
| 102 | + |
| 103 | + .. code-block:: yaml |
| 104 | + |
| 105 | + mongosh: |
| 106 | + inspectDepth: 20 |
| 107 | + |
| 108 | + During your ``mongosh`` session you run the following command to set |
| 109 | + ``inspectDepth`` to ``10``: |
| 110 | + |
| 111 | + .. code-block:: javascript |
| 112 | + |
| 113 | + config.set( "inspectDepth": 10 ) |
| 114 | + |
| 115 | + The value of ``inspectDepth`` becomes ``10``, and will remain ``10`` |
| 116 | + even when ``mongosh`` is restarted. |
| 117 | + |
| 118 | +Examples |
| 119 | +-------- |
| 120 | + |
| 121 | +Update Number of Items Returned by a Cursor |
| 122 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 123 | + |
| 124 | +Consider viewing a collection with a number of large documents. You can |
| 125 | +update the ``batchSize`` to limit the number of items returned by a |
| 126 | +cursor. |
| 127 | + |
| 128 | +.. code-block:: javascript |
| 129 | + |
| 130 | + config.set("displayBatchSize", 3) |
| 131 | + |
| 132 | + |
| 133 | +Future ``db.collection.find()`` operations will only return 3 documents |
| 134 | +per cursor iteration. |
| 135 | + |
| 136 | +Turn On Stack Traces |
| 137 | +~~~~~~~~~~~~~~~~~~~~ |
| 138 | + |
| 139 | +Enable stack traces to see more detailed error reporting. |
| 140 | + |
| 141 | +.. code-block:: javascript |
| 142 | + |
| 143 | + config.set("showStackTraces", true) |
| 144 | + |
| 145 | +The output differs like this: |
| 146 | + |
| 147 | +.. code-block:: javascript |
| 148 | + :copyable: false |
| 149 | + |
| 150 | + // showStackTraces set to 'false' |
| 151 | + Enterprise> db.orders.find( {}, { $thisWontWork: 1 } ) |
| 152 | + MongoError: FieldPath field names may not start with '$'. |
| 153 | + |
| 154 | + // showStackTraces set to 'true' |
| 155 | + Enterprise> db.orders.find( {}, { $thisWontWork: 1 } ) |
| 156 | + Uncaught: |
| 157 | + MongoError: FieldPath field names may not start with '$'. |
| 158 | + at MessageStream.messageHandler (/usr/bin/mongosh:58878:20) |
| 159 | + at MessageStream.emit (events.js:315:20) |
| 160 | + at MessageStream.EventEmitter.emit (domain.js:548:15) |
| 161 | + at processIncomingData (/usr/bin/mongosh:57954:12) |
| 162 | + at MessageStream._write (/usr/bin/mongosh:57850:5) |
| 163 | + at writeOrBuffer (_stream_writable.js:352:12) |
| 164 | + at MessageStream.Writable.write (_stream_writable.js:303:10) |
| 165 | + at Socket.ondata (_stream_readable.js:719:22) |
| 166 | + at Socket.emit (events.js:315:20) |
| 167 | + at Socket.EventEmitter.emit (domain.js:548:15) |
| 168 | + |
| 169 | +Call ``config`` API from outside :binary:`~bin.mongosh` |
| 170 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 171 | + |
| 172 | +You can call the ``config`` API from the command line using |
| 173 | +:option:`--eval` with :binary:`~bin.mongosh`. In this case the |
| 174 | +:option:`--nodb` option means :binary:`~bin.mongosh` will update |
| 175 | +without connecting to a MongoDB database. |
| 176 | + |
| 177 | +.. important:: |
| 178 | + |
| 179 | + You must use different quotation marks for the :option:`--eval` |
| 180 | + expression and the ``config`` property. That is, single quotes for |
| 181 | + one and double quotes for the other. |
| 182 | + |
| 183 | +.. code-block:: |
| 184 | + |
| 185 | + mongosh --nodb --eval 'config.set("enableTelemetry", true)' |
| 186 | + |
| 187 | +:binary:`~bin.mongosh` returns additional information along with the |
| 188 | +result of the API call. |
| 189 | + |
| 190 | +.. code-block:: |
| 191 | + :copyable: false |
| 192 | + |
| 193 | + Current Mongosh Log ID: 609583b730e14918fa0d363f |
| 194 | + Using MongoDB: undefined |
| 195 | + Using Mongosh Beta: 0.12.1 |
| 196 | + |
| 197 | + For mongosh info see: https://docs.mongodb.com/mongodb-shell/ |
| 198 | + |
| 199 | + Setting "enableTelemetry" has been changed |
| 200 | + |
| 201 | +Redact Sensitive Information |
| 202 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 203 | + |
| 204 | +Compare the recalled history when ``redactHistory`` is set to |
| 205 | +``remove-redact`` or ``remove``. |
| 206 | + |
| 207 | +Set ``redactHistory`` to ``remove-redact`` mode and enter a query |
| 208 | +containing an email address. |
| 209 | + |
| 210 | +.. code-block:: javascript |
| 211 | + |
| 212 | + config.set( "redactHistory", "remove-redact" ) |
| 213 | + db.contacts.find( {"email": " [email protected]" } ) |
| 214 | + |
| 215 | +When you press the ``up arrow`` to replay the last command the email |
| 216 | +address is redacted. |
| 217 | + |
| 218 | +.. code-block:: javascript |
| 219 | + :copyable: false |
| 220 | + |
| 221 | + db.contacts.find( {"email": "<email>" } ) // Redacted |
| 222 | + |
| 223 | +Set ``redactHistory`` to ``remove`` mode and enter a query containing |
| 224 | +an email address. |
| 225 | + |
| 226 | +.. code-block:: javascript |
| 227 | + |
| 228 | + config.set( "redactHistory", "remove" ) |
| 229 | + db.contacts.find( {"email": " [email protected]" } ) |
| 230 | + |
| 231 | +When you press the ``up arrow`` to replay the last command the email |
| 232 | +address is present. |
| 233 | + |
| 234 | +.. code-block:: javascript |
| 235 | + :copyable: false |
| 236 | + |
| 237 | + db.contacts.find( {"email": " [email protected]" } ) |
| 238 | + |
| 239 | +The :ref:`shell history <mdb-shell-command-history>` reflects the |
| 240 | +changes. (Note that this stores the most recent input first.) |
| 241 | + |
| 242 | +.. code-block:: javascript |
| 243 | + :copyable: false |
| 244 | + |
| 245 | + db.contacts.find( {"email": " [email protected]" } ) |
| 246 | + config.set( "redactHistory", "remove" ) |
| 247 | + db.contacts.find( {"email": "<email>" } ) |
| 248 | + config.set( "redactHistory", "remove-redact" ) |
| 249 | + |
| 250 | +.. _example-reset-setting: |
| 251 | + |
| 252 | +Reset a Configuration Setting to the Default Value |
| 253 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 254 | + |
| 255 | +If you modified a configuration setting and want to reset it to the |
| 256 | +default value, use ``config.reset( "<property>" )``. |
| 257 | + |
| 258 | +#. Change the value of the ``historyLength`` setting to ``2000``: |
| 259 | + |
| 260 | + .. code-block:: javascript |
| 261 | + |
| 262 | + config.set("historyLength", 2000) |
| 263 | + |
| 264 | +#. Verify the updated value for ``historyLength``: |
| 265 | + |
| 266 | + .. code-block:: javascript |
| 267 | + |
| 268 | + config.get("historyLength") |
| 269 | + |
| 270 | +#. Reset the ``historyLength`` setting to the default value of ``1000``: |
| 271 | + |
| 272 | + .. code-block:: javascript |
| 273 | + |
| 274 | + config.reset("historyLength") |
| 275 | + |
| 276 | +#. Verify the updated value for ``historyLength``: |
| 277 | + |
| 278 | + .. code-block:: javascript |
| 279 | + |
| 280 | + config.get("historyLength") |
0 commit comments