Skip to content

Commit 38f96d8

Browse files
committed
Better clarify Database::command() purpose and examples
Also fixes two long-standing syntax errors in the example code
1 parent 65b19c4 commit 38f96d8

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

docs/reference/method/MongoDBDatabase-command.txt

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ Definition
1515

1616
.. phpmethod:: MongoDB\\Database::command()
1717

18-
Execute a :manual:`command </reference/command>` on the database.
18+
Execute a :manual:`command </reference/command>` on the database. This is
19+
generally used to execute commands that do not have a corresponding helper
20+
method within the library.
1921

2022
.. code-block:: php
2123

@@ -43,9 +45,10 @@ Errors/Exceptions
4345
Example
4446
-------
4547

46-
The following example executes a :manual:`ping
47-
</reference/command/ping>` command, which returns a cursor with a single
48-
result document:
48+
Most database commands return a single result document, which can be obtained by
49+
converting the returned cursor to an array and accessing its first element. The
50+
following example executes a :manual:`ping </reference/command/ping>` command
51+
and prints its result document:
4952

5053
.. code-block:: php
5154

@@ -55,7 +58,7 @@ result document:
5558

5659
$cursor = $database->command(['ping' => 1]);
5760

58-
var_dump($c->toArray()[0]);
61+
var_dump($cursor->toArray()[0]);
5962

6063
The output would resemble:
6164

@@ -69,9 +72,11 @@ The output would resemble:
6972
}
7073
}
7174

72-
The following example executes a :manual:`listCollections
73-
</reference/command/listCollections>` command, which returns a cursor with
74-
multiple result documents:
75+
Some database commands return a cursor with multiple results. The following
76+
example executes :manual:`listCollections </reference/command/listCollections>`,
77+
which returns a cursor containing a result document for each collection in the
78+
``test`` database. Note that this example is illustrative; applications would
79+
generally use :phpmethod:`MongoDB\\Database::listCollections()` in practice.
7580

7681
.. code-block:: php
7782

@@ -81,7 +86,7 @@ multiple result documents:
8186

8287
$cursor = $database->command(['listCollections' => 1]);
8388

84-
var_dump($c->toArray());
89+
var_dump($cursor->toArray());
8590

8691
The output would resemble:
8792

0 commit comments

Comments
 (0)