|
| 1 | +.. _php-run-command: |
| 2 | + |
| 3 | +====================== |
| 4 | +Run a Database Command |
| 5 | +====================== |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. facet:: |
| 14 | + :name: genre |
| 15 | + :values: reference |
| 16 | + |
| 17 | +.. meta:: |
| 18 | + :keywords: administration, code example |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn how to use the {+php-library+} |
| 24 | +to run a database command. You can use database commands to perform a |
| 25 | +variety of administrative and diagnostic tasks, such as fetching server |
| 26 | +statistics, initializing a replica set, or running an aggregation pipeline. |
| 27 | + |
| 28 | +.. important:: Prefer Library Methods to Database Commands |
| 29 | + |
| 30 | + The library provides wrapper methods for many database commands. |
| 31 | + We recommend using these methods instead of executing database |
| 32 | + commands when possible. |
| 33 | + |
| 34 | + To perform administrative tasks, use the :mongosh:`MongoDB Shell </>` |
| 35 | + instead of the {+php-library+}. The shell provides helper methods |
| 36 | + that might not be available in the library. |
| 37 | + |
| 38 | + If there are no available helpers in the library or the shell, you |
| 39 | + can use the ``db.runCommand()`` shell method or the library's |
| 40 | + ``MongoDB\Database::command()`` method, which is described in this |
| 41 | + guide. |
| 42 | + |
| 43 | +.. _php-execute-command: |
| 44 | + |
| 45 | +Execute a Command |
| 46 | +----------------- |
| 47 | + |
| 48 | +To run a database command, you must specify the command and any relevant |
| 49 | +parameters in a command document, then pass the command document to the |
| 50 | +``MongoDB\Database::command()`` method. Many database commands return |
| 51 | +multiple result documents, so the ``command()`` method returns a |
| 52 | +:php:`MongoDB\Driver\Cursor <mongodb-driver-cursor>` object that you can |
| 53 | +iterate through. |
| 54 | + |
| 55 | +The following code shows how you can use the ``command()`` |
| 56 | +method on a :phpclass:`MongoDB\Database` instance to run the ``hello`` |
| 57 | +command, which returns information about the server: |
| 58 | + |
| 59 | +.. literalinclude:: /includes/write/run-command.php |
| 60 | + :language: php |
| 61 | + :dedent: |
| 62 | + :start-after: start-hello |
| 63 | + :end-before: end-hello |
| 64 | + |
| 65 | +To find a link to a full list of database commands and corresponding |
| 66 | +parameters, see the :ref:`Additional Information section |
| 67 | +<php-addtl-info-runcommand>`. |
| 68 | + |
| 69 | +.. note:: Read Preference |
| 70 | + |
| 71 | + The ``command()`` method does not inherit the read preference you might |
| 72 | + have set on your ``Database`` instance elsewhere in your code. By |
| 73 | + default, ``command()`` uses the ``primary`` read preference. |
| 74 | + |
| 75 | + You can set a read preference for command execution by setting one |
| 76 | + in the options parameter, as shown in the following code: |
| 77 | + |
| 78 | + .. literalinclude:: /includes/write/run-command.php |
| 79 | + :language: php |
| 80 | + :dedent: |
| 81 | + :start-after: start-readpref |
| 82 | + :end-before: end-readpref |
| 83 | + |
| 84 | + Learn more about the ``ReadPreference`` class in the |
| 85 | + :php:`{+extension-short+} API documentation |
| 86 | + <mongodb-driver-readpreference>`. |
| 87 | + |
| 88 | + To learn more about read preference options, see :manual:`Read |
| 89 | + Preference </core/read-preference/>` in the {+mdb-server+} manual. |
| 90 | + |
| 91 | +.. _php-command-response: |
| 92 | + |
| 93 | +Response |
| 94 | +-------- |
| 95 | + |
| 96 | +The ``command()`` method returns a ``Cursor`` object that contains |
| 97 | +the response from the database for the given command. Each database |
| 98 | +command performs a different function, so the response |
| 99 | +content can vary. |
| 100 | + |
| 101 | +For commands that return a single result document, |
| 102 | +that result is available as the first and only document in the |
| 103 | +cursor. For commands that return multiple result |
| 104 | +documents, the library converts the cursor |
| 105 | +envelope in the raw command response, which includes the cursor ID and |
| 106 | +the first batch of results, into an iterable cursor. |
| 107 | + |
| 108 | +Before you run a command, learn about the response format of the |
| 109 | +command so that your application either iterates through multiple |
| 110 | +results or extracts the first and only document in the cursor. See the |
| 111 | +:ref:`php-addtl-info-runcommand` section of this guide to find a link to |
| 112 | +the full list of database commands. |
| 113 | + |
| 114 | +The raw command response contains the following fields: |
| 115 | + |
| 116 | +.. list-table:: |
| 117 | + :header-rows: 1 |
| 118 | + :widths: 30 70 |
| 119 | + |
| 120 | + * - Field |
| 121 | + - Description |
| 122 | + |
| 123 | + * - <command result> |
| 124 | + - Fields specific to the database command. For example, |
| 125 | + the ``count`` command returns the ``n`` field. |
| 126 | + |
| 127 | + * - ``ok`` |
| 128 | + - Whether the command has succeeded (``1``) or failed (``0``). The |
| 129 | + library raises a :php:`CommandException |
| 130 | + <mongodb-driver-exception-commandexception.php>` if |
| 131 | + the ``ok`` field is ``0``. |
| 132 | + |
| 133 | + * - ``operationTime`` |
| 134 | + - The logical time of the operation. MongoDB uses the |
| 135 | + logical time to order operations. To learn more about this |
| 136 | + concept, see our blog post about the :website:`Global Logical |
| 137 | + Clock </blog/post/transactions-background-part-4-the-global-logical-clock>`. |
| 138 | + |
| 139 | + * - ``$clusterTime`` |
| 140 | + - A document that contains the signed cluster time. Cluster time is a |
| 141 | + logical time used for the ordering of operations. |
| 142 | + |
| 143 | +.. _php-command-example: |
| 144 | + |
| 145 | +Command Example |
| 146 | +--------------- |
| 147 | + |
| 148 | +The following example uses the ``command()`` method to run |
| 149 | +the ``dbStats`` command to retrieve storage statistics for the |
| 150 | +``accounts`` database: |
| 151 | + |
| 152 | +.. literalinclude:: /includes/write/run-command.php |
| 153 | + :language: php |
| 154 | + :dedent: |
| 155 | + :start-after: start-runcommand |
| 156 | + :end-before: end-runcommand |
| 157 | + |
| 158 | +The output of this command includes information about the collections in |
| 159 | +the database and describes the amount and size of data stored across |
| 160 | +collections: |
| 161 | + |
| 162 | +.. code-block:: none |
| 163 | + |
| 164 | + {"db":"accounts","collections":2,"views":0,"objects":5,"avgObjSize":22,"dataSize":110, |
| 165 | + "storageSize":40960,"totalFreeStorageSize":0,"numExtents":0,"indexes":2,"indexSize":40960, |
| 166 | + "indexFreeStorageSize":0,"fileSize":0,"nsSizeMB":0,"ok":1} |
| 167 | + |
| 168 | +.. _php-addtl-info-runcommand: |
| 169 | + |
| 170 | +Additional Information |
| 171 | +---------------------- |
| 172 | + |
| 173 | +For more information about the concepts in this guide, see the following |
| 174 | +documentation in the {+mdb-server+} manual: |
| 175 | + |
| 176 | +- :manual:`db.runCommand() </reference/method/db.runCommand/>` |
| 177 | +- :manual:`Database Commands </reference/command/>` |
| 178 | +- :manual:`hello Command </reference/command/hello/>` |
| 179 | +- :manual:`dbStats Command </reference/command/dbStats/>` |
| 180 | + |
| 181 | +API Documentation |
| 182 | +~~~~~~~~~~~~~~~~~ |
| 183 | + |
| 184 | +For more information about the ``command()`` method, see the |
| 185 | +following {+php-library+} API documentation: |
| 186 | + |
| 187 | +- :phpmethod:`MongoDB\Database::command()` |
0 commit comments