Skip to content

Commit d8adc35

Browse files
authored
DOCSP-26229: improve-runcommand (mongodb#225)
* DOCSP-26229-improve-runcommand * fix attempt: admonition in table * MW PR fixes 1
1 parent 406b19e commit d8adc35

File tree

2 files changed

+74
-47
lines changed

2 files changed

+74
-47
lines changed

source/fundamentals/crud/run-command.txt

+71-44
Original file line numberDiff line numberDiff line change
@@ -9,52 +9,57 @@ Run a Command
99
.. contents:: On this page
1010
:local:
1111
:backlinks: none
12-
:depth: 1
12+
:depth: 2
1313
:class: singlecol
1414

1515
Overview
1616
--------
1717

18-
In this guide, you can learn how to run a **database command** with the
18+
In this guide, you can learn how to run a database command with the
1919
{+driver-short+}. You can use database commands to perform a variety of
2020
administrative and diagnostic tasks, such as fetching server statistics,
2121
initializing a replica set, or running an aggregation pipeline.
2222

23-
Compose a Command
23+
Execute a Command
2424
-----------------
2525

26-
The {+driver-short+} provides the following methods to run database
27-
commands:
26+
To run a database command, you must specify the command and any relevant
27+
parameters in a command document, then pass the command document to a
28+
wrapper method. The command document must be an order-preserving type
29+
such as ``bson.D``. The {+driver-short+} provides the following methods
30+
to run database commands:
2831

2932
- ``RunCommand()``, which returns the command response as a
3033
``SingleResult`` type. You can use this method with any database command.
3134
- ``RunCommandCursor()``, which returns the command response as a
3235
``Cursor`` type. You can use this method if your database command
3336
returns multiple result documents.
3437

35-
Each method requires a Context and a command document. The
36-
command document must be an order-preserving type such as ``bson.D``.
37-
The following code shows how you can use the ``RunCommandCursor()``
38-
method to run the ``listCollections`` command on a database:
38+
The following code shows how you can use the ``RunCommand()``
39+
method to run the ``hello`` command, which returns information about
40+
the current member's role in the replica set, on a database:
3941

4042
.. code-block:: go
4143

42-
command := bson.D{{"listCollections", 1}}
43-
cursor, err := db.RunCommandCursor(context.TODO(), command)
44+
command := bson.D{{"hello", 1}}
4445

45-
For a full list of database commands, see the :ref:`Additional
46-
Information <addl-info-runcommand>`.
46+
var result bson.M
47+
err = db.RunCommand(context.TODO(), command).Decode(&result)
48+
49+
For a full list of database commands and corresponding parameters, see
50+
the :ref:`Additional Information section <addl-info-runcommand>`.
4751

4852
.. note:: Read Preference
4953

5054
``RunCommand()`` and ``RunCommandCursor()`` do not obey the read
51-
preference set for the database. You can set a read preference for a
52-
database command by passing a ``RunCmdOptions`` type to either method:
55+
preference you may have set on your ``Database`` object elsewhere in
56+
your code. You can set a read preference for command execution by
57+
passing a ``RunCmdOptions`` object to either method:
5358

5459
.. code-block:: go
5560

5661
opts := options.RunCmd().SetReadPreference(readpref.Primary())
57-
err := db.RunCommand(context.TODO(), command, opts).Decode(&result)
62+
cursor, err := db.RunCommandCursor(context.TODO(), command, opts)
5863

5964
For more information on
6065
read preference options, see the :ref:`golang-write-read-pref`
@@ -63,8 +68,11 @@ Information <addl-info-runcommand>`.
6368
Response
6469
--------
6570

66-
Each method returns a document or a cursor containing documents with the
67-
following fields:
71+
Each method returns a ``SingleResult`` object or a cursor that contains
72+
the response from the database after the command has been executed. Each
73+
database command performs a different function, so the response content
74+
can vary across commands. However, every response contains documents
75+
with the following fields:
6876

6977
.. list-table::
7078
:header-rows: 1
@@ -83,9 +91,13 @@ following fields:
8391
or failed (``0``).
8492

8593
* - ``operationTime``
86-
- Indicates the :website:`logical time
87-
</blog/post/transactions-background-part-4-the-global-logical-clock>`
88-
of the operation. MongoDB uses the logical time to order operations.
94+
- Indicates the logical time of the operation. MongoDB uses the
95+
logical time to order operations.
96+
97+
.. seealso::
98+
99+
To learn more about logical time, see our :website:`blog post about
100+
the Global Logical Clock </blog/post/transactions-background-part-4-the-global-logical-clock>`.
89101

90102
* - ``$clusterTime``
91103
- Provides a document that returns the signed cluster time. Cluster time is a
@@ -101,9 +113,9 @@ Example
101113
-------
102114

103115
The following code shows how you can use the ``RunCommand()`` method to
104-
run the ``count`` command on the ``flowers`` collection of the
105-
``plants`` database. This example searches for documents with a
106-
``price`` property less than or equal to 9.99.
116+
run the ``explain`` command for a ``count`` operation on the ``flowers`` collection of the
117+
``plants`` database. The ``explain`` command runs in the
118+
``"queryPlanner"`` verbosity mode:
107119

108120
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/runCommand.go
109121
:language: go
@@ -114,32 +126,48 @@ run the ``count`` command on the ``flowers`` collection of the
114126
Output
115127
~~~~~~
116128

117-
In the output, you should see the number of documents in
118-
the collection that match the query as the value of the ``n`` field, as
119-
well as the command execution information:
129+
In the output, you should see fields explaining the
130+
execution of the ``count`` operation, such as the winning plan, which is
131+
the plan selected by the query optimizer, and any rejected
132+
plans. The output also contains information about the execution of the
133+
``explain`` command:
120134

121135
.. code-block:: json
122-
:emphasize-lines: 15
136+
:emphasize-lines: 9-13,19-29
123137

124138
{
125139
"$clusterTime": {
126140
"clusterTime": {
127-
"T": 1666967516,
128-
"I": 32
141+
"T": 1673969525,
142+
"I": 24
129143
},
130-
"signature": {
131-
"hash": {
132-
"Subtype": 0,
133-
"Data": "..."
134-
},
135-
"keyId": 7120249010111119362
136-
}
144+
"signature": {...}
145+
},
146+
"command": {
147+
"$db": "plants",
148+
"count": "flowers"
137149
},
138-
"n": 47,
150+
"explainVersion": "1",
139151
"ok": 1,
140152
"operationTime": {
141-
"T": 1666967516,
142-
"I": 32
153+
"T": 1673969525,
154+
"I": 24
155+
},
156+
"queryPlanner": {
157+
"indexFilterSet": false,
158+
"maxIndexedAndSolutionsReached": false,
159+
"maxIndexedOrSolutionsReached": false,
160+
"maxScansToExplodeReached": false,
161+
"namespace": "plants.flowers",
162+
"rejectedPlans": [],
163+
"winningPlan": {
164+
"stage": "RECORD_STORE_FAST_COUNT"
165+
}
166+
},
167+
"serverInfo": {...},
168+
"serverParameters": {
169+
"internalDocumentSourceGroupMaxMemoryBytes": 104857600,
170+
...
143171
}
144172
}
145173

@@ -152,8 +180,8 @@ For more information about the concepts in this guide, see the following documen
152180

153181
- :manual:`db.runCommand() </reference/method/db.runCommand/>`
154182
- :manual:`Database Commands </reference/command/>`
155-
- :manual:`listCollections Command </reference/command/listCollections/>`
156-
- :manual:`count Command </reference/command/count/>`
183+
- :manual:`hello Command </reference/command/hello/>`
184+
- :manual:`explain Command </reference/command/explain/>`
157185

158186
To learn how to retrieve data from a cursor, see the
159187
:ref:`golang-cursor` fundamentals page.
@@ -163,5 +191,4 @@ API Documentation
163191

164192
- `RunCommand() <{+api+}/mongo#Database.RunCommand>`__
165193
- `RunCommandCursor() <{+api+}/mongo#Database.RunCommandCursor>`__
166-
- `RunCmdOptions <{+api+}/mongo/options#RunCmdOptions>`__
167-
- `Cursor <{+api+}/mongo#Cursor>`__
194+
- `RunCmdOptions <{+api+}/mongo/options#RunCmdOptions>`__

source/includes/fundamentals/code-snippets/CRUD/runCommand.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ func main() {
3131

3232
// start-runcommand
3333
db := client.Database("plants")
34-
filter := bson.D{{"price", bson.D{{"$lte", 9.99}}}}
35-
command := bson.D{{"count", "flowers"}, {"query", filter}}
34+
countCommand := bson.D{{"count", "flowers"}}
35+
explainCommand := bson.D{{"explain", countCommand}, {"verbosity", "queryPlanner"}}
3636

3737
var result bson.M
38-
err = db.RunCommand(context.TODO(), command).Decode(&result)
38+
err = db.RunCommand(context.TODO(), explainCommand).Decode(&result)
3939
// end-runcommand
4040

4141
if err != nil {

0 commit comments

Comments
 (0)