@@ -48,11 +48,13 @@ parameters in a command document, then pass the command document to the
48
48
``Cursor`` object.
49
49
50
50
The following code shows how you can use the ``command()``
51
- method on a database to run the ``hello`` command, which returns
52
- information about the current member's role in the replica set:
51
+ method on a ``MongoDB\Driver\Database`` instance to run the ``hello``
52
+ command, which returns information about the current member's role in
53
+ the replica set:
53
54
54
55
.. code-block:: php
55
56
57
+ $database = $client->selectDatabase('<db>');
56
58
$result = $database->command(['hello' => 1]);
57
59
58
60
To find a link to a full list of database commands and corresponding
@@ -61,10 +63,9 @@ parameters, see the :ref:`Additional Information section
61
63
62
64
.. note:: Read Preference
63
65
64
- The ``command()`` method does not
65
- obey the read preference you might have set on your ``Database``
66
- instance elsewhere in your code. By default, ``command()`` uses the ``primary``
67
- read preference.
66
+ The ``command()`` method does not obey the read preference you might
67
+ have set on your ``Database`` instance elsewhere in your code. By
68
+ default, ``command()`` uses the ``primary`` read preference.
68
69
69
70
You can set a read preference for command execution by setting one
70
71
in the options parameter, as shown in the following code:
@@ -130,32 +131,23 @@ Command Example
130
131
---------------
131
132
132
133
The following example uses the ``command()`` method to run
133
- the ``explain `` command for a ``count`` operation. The ``explain``
134
- command runs in the ``queryPlanner `` verbosity mode :
134
+ the ``dbStats `` command to retrieve storage statistics for the
135
+ ``accounts `` database :
135
136
136
137
.. literalinclude:: /includes/write/run-command.php
137
138
:language: php
138
139
:dedent:
139
140
:start-after: start-runcommand
140
141
:end-before: end-runcommand
141
142
142
- The output of this command includes fields explaining the
143
- execution of the ``count`` operation, such as the winning plan, which is
144
- the plan selected by the query optimizer, and any rejected
145
- plans. The output also contains information about the execution of the
146
- ``explain`` command:
143
+ The output of this command includes information about the collections in
144
+ the database and describes the amount and size of data stored across
145
+ collections:
147
146
148
147
.. code-block:: none
149
- :emphasize-lines: 2, 5-6
150
148
151
- string(1203)
152
- "[{"explainVersion":"1","queryPlanner":{"namespace":"plants.flowers",
153
- "indexFilterSet":false,"maxIndexedOrSolutionsReached":false,
154
- "maxIndexedAndSolutionsReached":false,"maxScansToExplodeReached":false,"winningPlan":
155
- {"stage":"RECORD_STORE_FAST_COUNT"},"rejectedPlans":[]},"command":{"count":"flowers",
156
- "$db":"plants"},"serverInfo":{"host":"...","port":27017,"version":"7.0.12",
157
- "gitVersion":"..."},"serverParameters":{...},"ok":1,"$clusterTime":{...},"signature":{...},
158
- "keyId":...}},"operationTime":{"$timestamp":{"t":1725637892,"i":12}}}]"
149
+ string(234)
150
+ "[{"db":"accounts","collections":2,"views":0,"objects":5,"avgObjSize":22,"dataSize":110,"storageSize":8192,"totalFreeStorageSize":0,"numExtents":0,"indexes":2,"indexSize":8192,"indexFreeStorageSize":0,"fileSize":0,"nsSizeMB":0,"ok":1}]"
159
151
160
152
.. _php-addtl-info-runcommand:
161
153
@@ -168,7 +160,7 @@ documentation in the {+mdb-server+} manual:
168
160
- :manual:`db.runCommand() </reference/method/db.runCommand/>`
169
161
- :manual:`Database Commands </reference/command/>`
170
162
- :manual:`hello Command </reference/command/hello/>`
171
- - :manual:`explain Command </reference/command/explain />`
163
+ - :manual:`dbStats Command </reference/command/dbStats />`
172
164
173
165
API Documentation
174
166
~~~~~~~~~~~~~~~~~
@@ -178,8 +170,10 @@ following {+php-library+} API documentation:
178
170
179
171
- `MongoDB\\Database::command() <{+api+}/method/MongoDBDatabase-command/>`__
180
172
181
- For more information about the ``MongoDB\Driver\ ReadPreference`` class ,
173
+ For more information about the ``Cursor`` and `` ReadPreference`` classes ,
182
174
see the following {+extension-short+} API documentation:
183
175
176
+ - `MongoDB\\Driver\\Cursor
177
+ <https://www.php.net/manual/en/class.mongodb-driver-cursor.php>`__
184
178
- `MongoDB\\Driver\\ReadPreference
185
179
<https://www.php.net/manual/en/class.mongodb-driver-readpreference.php>`__
0 commit comments