|
| 1 | +.. cpp-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 | +Execute a Command |
| 34 | +----------------- |
| 35 | + |
| 36 | +To run a database command, call the ``run_command()`` execution method on a ``mongocxx::database`` instance |
| 37 | +and pass in a document that specifies the command and any relevant arguments. |
| 38 | +The method returns the result of the command as a ``bsoncxx::document::value`` object. |
| 39 | + |
| 40 | +You can use the ``run_command()`` method with any database command. |
| 41 | +For a full list of database commands and corresponding parameters, see :manual:`Database Commands </reference/command/>` in the {+mdb-server+} manual. |
| 42 | + |
| 43 | +The following example shows how you can use the ``run_command()`` |
| 44 | +method to run the ``hello`` command on a database, which returns information about |
| 45 | +the current member's role in the replica set: |
| 46 | + |
| 47 | +.. io-code-block:: |
| 48 | + :copyable: true |
| 49 | + |
| 50 | + .. input:: /includes/run-command.cpp |
| 51 | + :start-after: start-run-hello |
| 52 | + :end-before: end-run-hello |
| 53 | + :language: cpp |
| 54 | + :dedent: |
| 55 | + |
| 56 | + .. output:: |
| 57 | + :language: cli |
| 58 | + :visible: false |
| 59 | + |
| 60 | + { |
| 61 | + "topologyVersion" : { |
| 62 | + "processId" : ..., |
| 63 | + "counter" : ... |
| 64 | + }, |
| 65 | + "hosts" : [ ... ], |
| 66 | + "setName" : ..., |
| 67 | + "setVersion" : ..., |
| 68 | + "isWritablePrimary" : ..., |
| 69 | + "secondary" : ..., |
| 70 | + "primary" : ..., |
| 71 | + "tags" : { |
| 72 | + "region" : ..., |
| 73 | + "availabilityZone" : ..., |
| 74 | + "provider" : ..., |
| 75 | + "workloadType" : ..., |
| 76 | + "nodeType" : ..., |
| 77 | + "diskState" : ... |
| 78 | + }, |
| 79 | + "me" : ..., |
| 80 | + "electionId" : ..., |
| 81 | + "lastWrite" : ..., |
| 82 | + "lastWriteDate" : ..., |
| 83 | + "majorityOpTime" : ..., |
| 84 | + "majorityWriteDate" : ..., |
| 85 | + "maxBsonObjectSize" : ..., |
| 86 | + "maxMessageSizeBytes" : ..., |
| 87 | + "maxWriteBatchSize" : ..., |
| 88 | + "localTime" : ..., |
| 89 | + "logicalSessionTimeoutMinutes" : ..., |
| 90 | + "connectionId" : ..., |
| 91 | + "minWireVersion" : ..., |
| 92 | + "maxWireVersion" : ..., |
| 93 | + "readOnly" : ..., |
| 94 | + "ok" : ..., |
| 95 | + "$clusterTime" : ..., |
| 96 | + "signature" : ... |
| 97 | + } |
| 98 | + |
| 99 | +.. cpp-command-options: |
| 100 | + |
| 101 | +Command Options |
| 102 | +--------------- |
| 103 | + |
| 104 | +To customize command execution behavior, you can set options in the command |
| 105 | +document that you pass to the ``run_command()`` method. To learn more about |
| 106 | +a command and the options that it accepts, locate the command and follow the |
| 107 | +corresponding link on the :manual:`Database Commands </reference/command/>` page |
| 108 | +in the {+mdb-server+} manual. |
| 109 | + |
| 110 | +For example, you can instruct the ``connectionStatus`` command |
| 111 | +to return the full set of privileges that currently-authenticated users possess by setting the ``showPrivileges`` |
| 112 | +option to ``true`` in the command document: |
| 113 | + |
| 114 | +.. io-code-block:: |
| 115 | + :copyable: true |
| 116 | + |
| 117 | + .. input:: /includes/run-command.cpp |
| 118 | + :start-after: start-run-connectionStatus |
| 119 | + :end-before: end-run-connectionStatus |
| 120 | + :language: cpp |
| 121 | + :dedent: |
| 122 | + |
| 123 | + .. output:: |
| 124 | + :language: cli |
| 125 | + :visible: false |
| 126 | + |
| 127 | + { |
| 128 | + "authInfo" : { "authenticatedUsers" : [ { "user" : ..., "db" : ... } ], |
| 129 | + "authenticatedUserRoles" : [ { "role" : ..., "db" : ... } ], |
| 130 | + "authenticatedUserPrivileges" : [ |
| 131 | + { "resource" : { "db" : "", "collection" : "" }, "actions" : [ ... ] }, |
| 132 | + { "resource" : { "db" : "config", "collection" : "system.sessions" }, "actions" : [ ... ] }, |
| 133 | + ..., |
| 134 | + { "resource" : { "db" : "", "collection" : "" }, "actions" : [ ... ] } |
| 135 | + ] |
| 136 | + }, |
| 137 | + "ok" : 1 |
| 138 | + } |
| 139 | + |
| 140 | +Response |
| 141 | +-------- |
| 142 | + |
| 143 | +The ``run_command()`` method returns a ``bsoncxx::document::value`` object that contains |
| 144 | +the response from the database after the command has been executed. Each |
| 145 | +database command performs a different function, so the response content |
| 146 | +can vary across commands. However, every response contains documents |
| 147 | +with the following fields: |
| 148 | + |
| 149 | +.. list-table:: |
| 150 | + :header-rows: 1 |
| 151 | + :widths: 30 70 |
| 152 | + |
| 153 | + * - Field |
| 154 | + - Description |
| 155 | + |
| 156 | + * - <command result> |
| 157 | + - Provides fields specific to the database command. For example, |
| 158 | + ``count`` returns the ``n`` field and ``explain`` returns the |
| 159 | + ``queryPlanner`` field. |
| 160 | + |
| 161 | + * - ``ok`` |
| 162 | + - Indicates whether the command has succeeded (``1``) |
| 163 | + or failed (``0``). |
| 164 | + |
| 165 | + * - ``operationTime`` |
| 166 | + - Indicates the logical time of the operation. MongoDB uses the |
| 167 | + logical time to order operations. |
| 168 | + |
| 169 | + * - ``$clusterTime`` |
| 170 | + - Provides a document that returns the signed cluster time. Cluster time is a |
| 171 | + logical time used for ordering of operations. |
| 172 | + |
| 173 | + The document contains the following fields: |
| 174 | + |
| 175 | + - ``clusterTime``, which is the timestamp of the highest known cluster time for the member. |
| 176 | + - ``signature``, which is a document that contains the hash of the cluster time and the ID |
| 177 | + of the key used to sign the cluster time. |
| 178 | + |
| 179 | +.. _addl-info-runcommand: |
| 180 | + |
| 181 | +Additional Information |
| 182 | +---------------------- |
| 183 | + |
| 184 | +For more information about the concepts in this guide, see the following documentation: |
| 185 | + |
| 186 | +- :manual:`db.runCommand() </reference/method/db.runCommand/>` |
| 187 | +- :manual:`Database Commands </reference/command/>` |
| 188 | +- :manual:`hello Command </reference/command/hello/>` |
| 189 | +- :manual:`connectionStatus Command </reference/command/connectionStatus/>` |
| 190 | + |
| 191 | +To learn more about the methods or types discussed in this |
| 192 | +guide, see the following API documentation: |
| 193 | + |
| 194 | +- `run_command() <{+api+}/classmongocxx_1_1v__noabi_1_1database.html#a1e11c0874c945f8bb9ca39f1a30c9271>`__ |
0 commit comments