Skip to content

Commit 160470c

Browse files
committed
JM tech review
1 parent 64ef4cb commit 160470c

File tree

2 files changed

+66
-41
lines changed

2 files changed

+66
-41
lines changed

source/includes/write/run-command.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,29 @@
44

55
use MongoDB\Client;
66

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

10+
// start-hello
11+
$database = $client->selectDatabase('myDB');
12+
$cursor = $database->command(['hello' => 1]);
13+
// end-hello
14+
15+
// start-readpref
16+
$readPref = new MongoDB\Driver\ReadPreference('primaryPreferred');
17+
$cursor = $database->command(
18+
['hello' => 1],
19+
['readPreference' => $readPref]
20+
);
21+
// end-readpref
22+
1023
// start-runcommand
1124
$database = $client->accounts;
1225
$command = ['dbStats' => 1];
1326

14-
$result = $database->command($command);
15-
// end-runcommand
27+
// dbStats returns a single document
28+
$cursor = $database->command($command);
1629

17-
var_dump(json_encode($result->toArray()));
30+
// Print the first document in the cursor
31+
echo json_encode($cursor->toArray()[0]), PHP_EOL;
32+
// end-runcommand

source/write/run-command.txt

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ statistics, initializing a replica set, or running an aggregation pipeline.
3232
commands when possible.
3333

3434
To perform administrative tasks, use the :mongosh:`MongoDB Shell </>`
35-
instead of the {+php-library+}. Calling the ``db.runCommand()``
36-
method inside the shell is the preferred way to issue database
37-
commands, as it provides a consistent interface between the shell and
38-
drivers or libraries.
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.
3940

4041
.. _php-execute-command:
4142

@@ -44,18 +45,20 @@ Execute a Command
4445

4546
To run a database command, you must specify the command and any relevant
4647
parameters in a command document, then pass the command document to the
47-
``MongoDB\Database::command()`` method. This method returns a
48-
``Cursor`` object.
48+
``MongoDB\Database::command()`` method. Many database commands return
49+
multiple result documents, so the ``command()`` method returns a
50+
``Cursor`` object that you can iterate through.
4951

5052
The following code shows how you can use the ``command()``
51-
method on a ``MongoDB\Driver\Database`` instance to run the ``hello``
53+
method on a :phpclass:`MongoDB\Database` instance to run the ``hello``
5254
command, which returns information about the current member's role in
5355
the replica set:
5456

55-
.. code-block:: php
56-
57-
$database = $client->selectDatabase('<db>');
58-
$result = $database->command(['hello' => 1]);
57+
.. literalinclude:: /includes/write/run-command.php
58+
:language: php
59+
:dedent:
60+
:start-after: start-hello
61+
:end-before: end-hello
5962

6063
To find a link to a full list of database commands and corresponding
6164
parameters, see the :ref:`Additional Information section
@@ -70,13 +73,11 @@ parameters, see the :ref:`Additional Information section
7073
You can set a read preference for command execution by setting one
7174
in the options parameter, as shown in the following code:
7275

73-
.. code-block:: php
74-
75-
$readPref = new MongoDB\Driver\ReadPreference('primaryPreferred');
76-
$result = $database->command(
77-
['hello' => 1],
78-
['readPreference' => $readPref]
79-
);
76+
.. literalinclude:: /includes/write/run-command.php
77+
:language: php
78+
:dedent:
79+
:start-after: start-readpref
80+
:end-before: end-readpref
8081

8182
For more information on read preference options, see :manual:`Read
8283
Preference </core/read-preference/>` in the {+mdb-server+} manual.
@@ -87,11 +88,25 @@ Response
8788
--------
8889

8990
The ``command()`` method returns a ``Cursor`` object that contains
90-
the response from the database after the command has been executed.
91-
92-
Each database command performs a different function, so the response
93-
content can vary depending on the command executed. However, every
94-
response contains a document with the following fields:
91+
the response from the database for the given command. Each database
92+
command performs a different function, so the response
93+
content can vary. Some command responses contain multiple result
94+
documents. In these situations, the library converts the cursor
95+
envelope in the raw command response, which includes the cursor ID and
96+
the first batch of results, into an iterable cursor.
97+
98+
Before you run a command, learn about the response format of the
99+
command so that your application either iterates through multiple
100+
results or extracts the first and only document in the cursor. See the
101+
:ref:`php-addtl-info-runcommand` section of this guide to find a link to
102+
the full list of database commands.
103+
104+
If the number of documents in the command response is sufficiently large, you
105+
can run a :manual:`getMore </reference/command/getMore/>` command to
106+
retrieve the next batch of results from the cursor by using the cursor
107+
ID.
108+
109+
The raw command response contains the following fields:
95110

96111
.. list-table::
97112
:header-rows: 1
@@ -102,29 +117,24 @@ response contains a document with the following fields:
102117

103118
* - <command result>
104119
- Fields specific to the database command. For example,
105-
``count`` returns the ``n`` field and ``explain`` returns the
106-
``queryPlanner`` field.
120+
the ``count`` command returns the ``n`` field.
107121

108122
* - ``ok``
109-
- Whether the command has succeeded (``1``)
110-
or failed (``0``).
123+
- Whether the command has succeeded (``1``) or failed (``0``). The
124+
library raises a :php:`CommandException
125+
<mongodb-driver-exception-commandexception.php>` if
126+
the ``ok`` field is ``0``.
111127

112128
* - ``operationTime``
113129
- The logical time of the operation. MongoDB uses the
114130
logical time to order operations. To learn more about this
115-
concept, see our blog post about the :website:`Global Logical
131+
concept, see our blog post about the :website:`Global Logical
116132
Clock </blog/post/transactions-background-part-4-the-global-logical-clock>`.
117133

118134
* - ``$clusterTime``
119135
- A document that contains the signed cluster time. Cluster time is a
120136
logical time used for the ordering of operations.
121137

122-
This document contains the following fields:
123-
124-
- ``clusterTime``, the timestamp of the highest known cluster time for the member
125-
- ``signature``, a document that contains the hash of the cluster time and the ID
126-
of the key used to sign the cluster time
127-
128138
.. _php-command-example:
129139

130140
Command Example
@@ -146,9 +156,9 @@ collections:
146156

147157
.. code-block:: none
148158

149-
string(234) "[{"db":"accounts","collections":2,"views":0,"objects":5,"avgObjSize":22,
150-
"dataSize":110,"storageSize":8192,"totalFreeStorageSize":0,"numExtents":0,"indexes":2,
151-
"indexSize":8192,"indexFreeStorageSize":0,"fileSize":0,"nsSizeMB":0,"ok":1}]"
159+
{"db":"accounts","collections":2,"views":0,"objects":5,"avgObjSize":22,"dataSize":110,
160+
"storageSize":40960,"totalFreeStorageSize":0,"numExtents":0,"indexes":2,"indexSize":40960,
161+
"indexFreeStorageSize":0,"fileSize":0,"nsSizeMB":0,"ok":1}
152162

153163
.. _php-addtl-info-runcommand:
154164

0 commit comments

Comments
 (0)