@@ -32,10 +32,11 @@ statistics, initializing a replica set, or running an aggregation pipeline.
32
32
commands when possible.
33
33
34
34
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.
39
40
40
41
.. _php-execute-command:
41
42
@@ -44,18 +45,20 @@ Execute a Command
44
45
45
46
To run a database command, you must specify the command and any relevant
46
47
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.
49
51
50
52
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``
52
54
command, which returns information about the current member's role in
53
55
the replica set:
54
56
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
59
62
60
63
To find a link to a full list of database commands and corresponding
61
64
parameters, see the :ref:`Additional Information section
@@ -70,13 +73,11 @@ parameters, see the :ref:`Additional Information section
70
73
You can set a read preference for command execution by setting one
71
74
in the options parameter, as shown in the following code:
72
75
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
80
81
81
82
For more information on read preference options, see :manual:`Read
82
83
Preference </core/read-preference/>` in the {+mdb-server+} manual.
@@ -87,11 +88,25 @@ Response
87
88
--------
88
89
89
90
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:
95
110
96
111
.. list-table::
97
112
:header-rows: 1
@@ -102,29 +117,24 @@ response contains a document with the following fields:
102
117
103
118
* - <command result>
104
119
- 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.
107
121
108
122
* - ``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``.
111
127
112
128
* - ``operationTime``
113
129
- The logical time of the operation. MongoDB uses the
114
130
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
116
132
Clock </blog/post/transactions-background-part-4-the-global-logical-clock>`.
117
133
118
134
* - ``$clusterTime``
119
135
- A document that contains the signed cluster time. Cluster time is a
120
136
logical time used for the ordering of operations.
121
137
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
-
128
138
.. _php-command-example:
129
139
130
140
Command Example
@@ -146,9 +156,9 @@ collections:
146
156
147
157
.. code-block:: none
148
158
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}
152
162
153
163
.. _php-addtl-info-runcommand:
154
164
0 commit comments