@@ -9,52 +9,57 @@ Run a Command
9
9
.. contents:: On this page
10
10
:local:
11
11
:backlinks: none
12
- :depth: 1
12
+ :depth: 2
13
13
:class: singlecol
14
14
15
15
Overview
16
16
--------
17
17
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
19
19
{+driver-short+}. You can use database commands to perform a variety of
20
20
administrative and diagnostic tasks, such as fetching server statistics,
21
21
initializing a replica set, or running an aggregation pipeline.
22
22
23
- Compose a Command
23
+ Execute a Command
24
24
-----------------
25
25
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:
28
31
29
32
- ``RunCommand()``, which returns the command response as a
30
33
``SingleResult`` type. You can use this method with any database command.
31
34
- ``RunCommandCursor()``, which returns the command response as a
32
35
``Cursor`` type. You can use this method if your database command
33
36
returns multiple result documents.
34
37
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:
39
41
40
42
.. code-block:: go
41
43
42
- command := bson.D{{"listCollections", 1}}
43
- cursor, err := db.RunCommandCursor(context.TODO(), command)
44
+ command := bson.D{{"hello", 1}}
44
45
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>`.
47
51
48
52
.. note:: Read Preference
49
53
50
54
``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:
53
58
54
59
.. code-block:: go
55
60
56
61
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)
58
63
59
64
For more information on
60
65
read preference options, see the :ref:`golang-write-read-pref`
@@ -63,8 +68,11 @@ Information <addl-info-runcommand>`.
63
68
Response
64
69
--------
65
70
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:
68
76
69
77
.. list-table::
70
78
:header-rows: 1
@@ -83,9 +91,13 @@ following fields:
83
91
or failed (``0``).
84
92
85
93
* - ``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>`.
89
101
90
102
* - ``$clusterTime``
91
103
- Provides a document that returns the signed cluster time. Cluster time is a
@@ -101,9 +113,9 @@ Example
101
113
-------
102
114
103
115
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:
107
119
108
120
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/runCommand.go
109
121
:language: go
@@ -114,32 +126,48 @@ run the ``count`` command on the ``flowers`` collection of the
114
126
Output
115
127
~~~~~~
116
128
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:
120
134
121
135
.. code-block:: json
122
- :emphasize-lines: 15
136
+ :emphasize-lines: 9-13,19-29
123
137
124
138
{
125
139
"$clusterTime": {
126
140
"clusterTime": {
127
- "T": 1666967516 ,
128
- "I": 32
141
+ "T": 1673969525 ,
142
+ "I": 24
129
143
},
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"
137
149
},
138
- "n ": 47 ,
150
+ "explainVersion ": "1" ,
139
151
"ok": 1,
140
152
"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
+ ...
143
171
}
144
172
}
145
173
@@ -152,8 +180,8 @@ For more information about the concepts in this guide, see the following documen
152
180
153
181
- :manual:`db.runCommand() </reference/method/db.runCommand/>`
154
182
- :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 />`
157
185
158
186
To learn how to retrieve data from a cursor, see the
159
187
:ref:`golang-cursor` fundamentals page.
@@ -163,5 +191,4 @@ API Documentation
163
191
164
192
- `RunCommand() <{+api+}/mongo#Database.RunCommand>`__
165
193
- `RunCommandCursor() <{+api+}/mongo#Database.RunCommandCursor>`__
166
- - `RunCmdOptions <{+api+}/mongo/options#RunCmdOptions>`__
167
- - `Cursor <{+api+}/mongo#Cursor>`__
194
+ - `RunCmdOptions <{+api+}/mongo/options#RunCmdOptions>`__
0 commit comments