|
| 1 | +.. _node-run-command: |
| 2 | + |
| 3 | +============= |
| 4 | +Run a Command |
| 5 | +============= |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +Overview |
| 14 | +-------- |
| 15 | + |
| 16 | +In this guide, you can learn how to run a database command with the |
| 17 | +{+driver-short+}. You can use database commands to perform a variety of |
| 18 | +administrative and diagnostic tasks, such as fetching server statistics, |
| 19 | +initializing a replica set, or running an aggregation pipeline. |
| 20 | + |
| 21 | +.. important:: Prefer Driver Methods to Database Commands |
| 22 | + |
| 23 | + The driver provides wrapper methods for many database commands. |
| 24 | + We recommend using driver methods instead of executing database |
| 25 | + commands when possible. |
| 26 | + |
| 27 | + To perform administrative tasks, use the :mongosh:`MongoDB Shell </>` |
| 28 | + instead of the {+driver-short+}. Calling the ``db.runCommand()`` |
| 29 | + method inside the shell is the preferred method to issue database |
| 30 | + commands, as it provides a consistent interface between the shell and |
| 31 | + drivers. |
| 32 | + |
| 33 | +.. tip:: |
| 34 | + |
| 35 | + Use the :mongosh:`MongoDB Shell </>` for |
| 36 | + administrative tasks instead of the Java driver whenever possible, |
| 37 | + since these tasks are often quicker and easier to implement with the |
| 38 | + shell than in a Java application. This is the preferred method to |
| 39 | + issue database commands, as it provides a consistent interface |
| 40 | + between the shell and drivers. |
| 41 | + |
| 42 | +Execute a Command |
| 43 | +----------------- |
| 44 | + |
| 45 | +To run a database command, you must specify the command and any relevant |
| 46 | +parameters in a command document, then pass the command document to a |
| 47 | +command execution method. The {+driver-short+} provides the following methods |
| 48 | +to run database commands: |
| 49 | + |
| 50 | +- ``command()``, which returns the command response as a |
| 51 | + ``Document`` type. You can use this method with any database command. |
| 52 | +- ``runCursorCommand()``, which returns the command response as an iterable |
| 53 | + ``RunCommandCursor`` type. You can use this method only if your database command |
| 54 | + returns multiple result documents. |
| 55 | + |
| 56 | +The following code shows how you can use the ``command()`` |
| 57 | +method to run the ``hello`` command, which returns information about |
| 58 | +the current member's role in the replica set, on a database: |
| 59 | + |
| 60 | +.. code-block:: javascript |
| 61 | + |
| 62 | + const result = await myDB.command({ hello: 1 }); |
| 63 | + |
| 64 | +For a full list of database commands and corresponding parameters, see |
| 65 | +the :ref:`Additional Information section <addl-info-runcommand>`. |
| 66 | + |
| 67 | +.. note:: Read Preference |
| 68 | + |
| 69 | + ``command()`` and ``runCursorCommand()`` do not obey the read |
| 70 | + preference you may have set on your ``Db`` object elsewhere in |
| 71 | + your code. By default, these methods use the ``primary`` read |
| 72 | + preference. |
| 73 | + |
| 74 | + You can set a read preference for command execution by |
| 75 | + passing an options object to either method. The ``command()`` method |
| 76 | + takes a ``RunCommandOptions`` object, and the ``runCursorCommand()`` method |
| 77 | + takes a ``RunCursorCommandOptions`` object. |
| 78 | + |
| 79 | + The following code shows how to specify a read preference and pass it |
| 80 | + as an option to the ``command()`` method: |
| 81 | + |
| 82 | + .. code-block:: javascript |
| 83 | + |
| 84 | + const commandOptions = { readPreference: "nearest" }; |
| 85 | + const result = await myDB.command(commandDoc, commandOptions); |
| 86 | + |
| 87 | + For more information on read preference options, see :manual:`Read |
| 88 | + Preference </core/read-preference/>` in the Server manual. |
| 89 | + |
| 90 | +Response |
| 91 | +-------- |
| 92 | + |
| 93 | +Each method returns a ``Document`` object or a cursor that contains |
| 94 | +the response from the database after the command has been executed. Each |
| 95 | +database command performs a different function, so the response content |
| 96 | +can vary across commands. However, every response contains documents |
| 97 | +with the following fields: |
| 98 | + |
| 99 | +.. list-table:: |
| 100 | + :header-rows: 1 |
| 101 | + :widths: 30 70 |
| 102 | + |
| 103 | + * - Field |
| 104 | + - Description |
| 105 | + |
| 106 | + * - <command result> |
| 107 | + - Provides fields specific to the database command. For example, |
| 108 | + ``count`` returns the ``n`` field and ``explain`` returns the |
| 109 | + ``queryPlanner`` field. |
| 110 | + |
| 111 | + * - ``ok`` |
| 112 | + - Indicates whether the command has succeeded (``1``) |
| 113 | + or failed (``0``). |
| 114 | + |
| 115 | + * - ``operationTime`` |
| 116 | + - Indicates the logical time of the operation. MongoDB uses the |
| 117 | + logical time to order operations. |
| 118 | + |
| 119 | + .. seealso:: |
| 120 | + |
| 121 | + To learn more about logical time, see our :website:`blog post about |
| 122 | + the Global Logical Clock </blog/post/transactions-background-part-4-the-global-logical-clock>`. |
| 123 | + |
| 124 | + * - ``$clusterTime`` |
| 125 | + - Provides a document that returns the signed cluster time. Cluster time is a |
| 126 | + logical time used for ordering of operations. |
| 127 | + |
| 128 | + The document contains the following fields: |
| 129 | + |
| 130 | + - ``clusterTime``, which is the timestamp of the highest known cluster time for the member. |
| 131 | + - ``signature``, which is a document that contains the hash of the cluster time and the ID |
| 132 | + of the key used to sign the cluster time. |
| 133 | + |
| 134 | +Example |
| 135 | +------- |
| 136 | + |
| 137 | +The following code shows how you can use the ``runCursorCommand()`` method to |
| 138 | +run the ``checkMetadataConsistency`` command on the ``testDB`` database |
| 139 | +and iterate through the results: |
| 140 | + |
| 141 | +.. literalinclude:: /code-snippets/crud/runCommand.js |
| 142 | + :language: javascript |
| 143 | + :dedent: |
| 144 | + :start-after: start-runcommand |
| 145 | + :end-before: end-runcommand |
| 146 | + |
| 147 | +Output |
| 148 | +~~~~~~ |
| 149 | + |
| 150 | +The output contains the contents of the cursor object. The documents |
| 151 | +describe any metadata inconsistencies in the database: |
| 152 | + |
| 153 | +.. code-block:: javascript |
| 154 | + |
| 155 | + { |
| 156 | + type: ..., |
| 157 | + description: ..., |
| 158 | + details: { |
| 159 | + namespace: ..., |
| 160 | + info: ... |
| 161 | + } |
| 162 | + } |
| 163 | + { |
| 164 | + type: ..., |
| 165 | + description: ..., |
| 166 | + details: { |
| 167 | + namespace: ..., |
| 168 | + collectionUUID: ..., |
| 169 | + maxKeyObj: ..., |
| 170 | + ... |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | +.. note:: |
| 175 | + |
| 176 | + If you store the command response in a cursor, you see only the |
| 177 | + command result documents when you access the contents of the cursor. You won't |
| 178 | + see the ``ok``, ``operationTime``, and ``$clusterTime`` fields. |
| 179 | + |
| 180 | +.. _addl-info-runcommand: |
| 181 | + |
| 182 | +Additional Information |
| 183 | +---------------------- |
| 184 | + |
| 185 | +For more information about the concepts in this guide, see the following documentation: |
| 186 | + |
| 187 | +- :manual:`db.runCommand() </reference/method/db.runCommand/>` |
| 188 | +- :manual:`Database Commands </reference/command/>` |
| 189 | +- :manual:`hello Command </reference/command/hello/>` |
| 190 | + |
| 191 | +.. - :manual:`checkMetadataConsistency Command </reference/command/checkMetadataConsistency/>` |
| 192 | + |
| 193 | +To learn how to retrieve data from a cursor, see the |
| 194 | +:ref:`node-access-cursor` fundamentals page. |
0 commit comments