File tree Expand file tree Collapse file tree 2 files changed +23
-0
lines changed Expand file tree Collapse file tree 2 files changed +23
-0
lines changed Original file line number Diff line number Diff line change @@ -104,6 +104,11 @@ it issues a warning.
104
104
Pressing ``Ctrl + C`` once will not exit :binary:`~bin.mongosh`, press
105
105
``Ctrl + C`` twice to exit :binary:`~bin.mongosh`.
106
106
107
+ You can also terminate a script from within the script code by calling the
108
+ ``exit(<code>)`` command. For more information, refer to
109
+ :ref:`Terminate a Script on Error <mongosh-terminate-script>`.
110
+
111
+
107
112
Command Exceptions
108
113
------------------
109
114
Original file line number Diff line number Diff line change @@ -271,6 +271,24 @@ You can also specify the shortened form of the options:
271
271
space it will not be saved in your command history. This minimizes
272
272
exposure if you enter passwords on the command line.
273
273
274
+
275
+ .. _mongosh-terminate-script:
276
+
277
+ Terminate a Script on Error
278
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
279
+
280
+ It is often useful to terminate a running script if an exception is thrown, or
281
+ in the case of unexpected results. Exiting at a specific point in the script
282
+ prevents the execution of unnecessary code and potentially unexpected results.
283
+
284
+ To terminate a script, you can call the ``exit(<code>)`` method, where the
285
+ ``<code>`` is any user-specified value.
286
+
287
+ As a best practice, wrap code in a ``try - catch``, calling the
288
+ ``exit`` method in the ``catch`` block. Likewise, to check the results of a query or any command,
289
+ you can add an ``if - else`` statement and call the ``exit`` method if the
290
+ results are not what is expected.
291
+
274
292
.. _mongosh-write-scripts-config-file:
275
293
276
294
Execute Code From a Configuration File
You can’t perform that action at this time.
0 commit comments