Skip to content

Commit 9616c2a

Browse files
committed
JT tech review 1
1 parent 344a891 commit 9616c2a

File tree

2 files changed

+24
-32
lines changed

2 files changed

+24
-32
lines changed

source/includes/write/run-command.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,14 @@
44

55
use MongoDB\Client;
66

7-
$client = new Client('<connection string>');
7+
$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset');
8+
$client = new MongoDB\Client($uri);
89

910
// start-runcommand
10-
$database = $client->plants;
11+
$database = $client->accounts;
12+
$command = ['dbStats' => 1];
1113

12-
$countCommand = ['count' => 'flowers'];
13-
$explainCommand = ['explain' => $countCommand, 'verbosity' => 'queryPlanner'];
14-
15-
$result = $database->command($explainCommand);
14+
$result = $database->command($command);
1615
// end-runcommand
1716

18-
var_dump($result->toArray());
19-
17+
var_dump(json_encode($result->toArray()));

source/write/run-command.txt

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,13 @@ parameters in a command document, then pass the command document to the
4848
``Cursor`` object.
4949

5050
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:
5354

5455
.. code-block:: php
5556

57+
$database = $client->selectDatabase('<db>');
5658
$result = $database->command(['hello' => 1]);
5759

5860
To find a link to a full list of database commands and corresponding
@@ -61,10 +63,9 @@ parameters, see the :ref:`Additional Information section
6163

6264
.. note:: Read Preference
6365

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.
6869

6970
You can set a read preference for command execution by setting one
7071
in the options parameter, as shown in the following code:
@@ -130,32 +131,23 @@ Command Example
130131
---------------
131132

132133
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:
135136

136137
.. literalinclude:: /includes/write/run-command.php
137138
:language: php
138139
:dedent:
139140
:start-after: start-runcommand
140141
:end-before: end-runcommand
141142

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:
147146

148147
.. code-block:: none
149-
:emphasize-lines: 2, 5-6
150148

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}]"
159151

160152
.. _php-addtl-info-runcommand:
161153

@@ -168,7 +160,7 @@ documentation in the {+mdb-server+} manual:
168160
- :manual:`db.runCommand() </reference/method/db.runCommand/>`
169161
- :manual:`Database Commands </reference/command/>`
170162
- :manual:`hello Command </reference/command/hello/>`
171-
- :manual:`explain Command </reference/command/explain/>`
163+
- :manual:`dbStats Command </reference/command/dbStats/>`
172164

173165
API Documentation
174166
~~~~~~~~~~~~~~~~~
@@ -178,8 +170,10 @@ following {+php-library+} API documentation:
178170

179171
- `MongoDB\\Database::command() <{+api+}/method/MongoDBDatabase-command/>`__
180172

181-
For more information about the ``MongoDB\Driver\ReadPreference`` class,
173+
For more information about the ``Cursor`` and ``ReadPreference`` classes,
182174
see the following {+extension-short+} API documentation:
183175

176+
- `MongoDB\\Driver\\Cursor
177+
<https://www.php.net/manual/en/class.mongodb-driver-cursor.php>`__
184178
- `MongoDB\\Driver\\ReadPreference
185179
<https://www.php.net/manual/en/class.mongodb-driver-readpreference.php>`__

0 commit comments

Comments
 (0)