Skip to content

Commit a8664ef

Browse files
committed
copy over from node
1 parent eb4b830 commit a8664ef

File tree

1 file changed

+214
-0
lines changed

1 file changed

+214
-0
lines changed
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
.. _kotlin-run-command:
2+
3+
=============
4+
Run a Command
5+
=============
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
13+
Overview
14+
--------
15+
16+
In this guide, you can learn how to run a database command with the
17+
{+driver-short+}. You can use database commands to perform a variety of
18+
administrative and diagnostic tasks, such as fetching server statistics,
19+
initializing a replica set, or running an aggregation pipeline.
20+
21+
.. important:: Prefer Driver Methods to Database Commands
22+
23+
The driver provides wrapper methods for many database commands.
24+
We recommend using driver methods instead of executing database
25+
commands when possible.
26+
27+
To perform administrative tasks, use the :mongosh:`MongoDB Shell </>`
28+
instead of the {+driver-short+}. Calling the ``db.runCommand()``
29+
method inside the shell is the preferred method to issue database
30+
commands, as it provides a consistent interface between the shell and
31+
drivers.
32+
33+
Execute a Command
34+
-----------------
35+
36+
To run a database command, you must specify the command and any relevant
37+
parameters in a document, then pass this document to a
38+
command execution method. The {+driver-short+} provides the following methods
39+
to run database commands:
40+
41+
- ``command()``, which returns the command response as a
42+
``Document`` type. You can use this method with any database command.
43+
- ``runCursorCommand()``, which returns the command response as an iterable
44+
``RunCommandCursor`` type. You can use this method only if your database command
45+
returns multiple result documents.
46+
47+
The following code shows how you can use the ``command()``
48+
method to run the ``hello`` command, which returns information about
49+
the current member's role in the replica set, on a database:
50+
51+
.. code-block:: javascript
52+
53+
const result = await myDB.command({ hello: 1 });
54+
55+
For a full list of database commands and corresponding parameters, see
56+
the :ref:`Additional Information section <addl-info-runcommand>`.
57+
58+
.. _kotlin-command-options:
59+
60+
Command Options
61+
---------------
62+
63+
You can specify optional command behavior for the ``command()``
64+
and ``runCursorCommand()`` methods.
65+
66+
The ``command()`` method accepts a ``RunCommandOptions`` object. To learn
67+
more about the ``RunCommandOptions`` type, see the `API documentation <{+api+}/types/RunCommandOptions.html>`__.
68+
69+
The ``runCursorCommand() method`` accepts a ``RunCursorCommandOptions``
70+
object. To learn more about the ``RunCursorCommandOptions`` type, see
71+
the `API documentation <{+api+}/types/RunCursorCommandOptions.html>`__.
72+
73+
Starting in version 6.0 of the {+driver-short+}, you can pass only the
74+
following options to the ``command()`` method:
75+
76+
- ``comment``
77+
- ``enableUtf8Validation``
78+
- ``raw``
79+
- ``readPreference``
80+
- ``session``
81+
82+
You can set more options in the document that you pass to the ``command()`` method. To
83+
learn more about a command and the options that it accepts, locate the command and follow
84+
the link on the :manual:`Database Commands </reference/command/>` section of the Server
85+
manual.
86+
87+
The following code shows how to specify a ``grantRolesToUser`` command
88+
that executes with a ``majority`` write concern:
89+
90+
.. code-block:: javascript
91+
:emphasize-lines: 4
92+
93+
const commandDoc = {
94+
grantRolesToUser: "user011",
95+
roles: [ "readWrite" ],
96+
writeConcern: { w: "majority" }
97+
};
98+
const result = await myDB.command(commandDoc);
99+
100+
.. note:: Read Preference
101+
102+
The ``command()`` and ``runCursorCommand()`` methods ignore
103+
the read preference setting you may have set on your ``Db`` object.
104+
By default, these methods use the ``primary`` read preference.
105+
106+
The following code shows how to specify a read preference and pass it
107+
as an option to the ``command()`` method:
108+
109+
.. code-block:: javascript
110+
111+
const commandOptions = { readPreference: "nearest" };
112+
const result = await myDB.command(commandDoc, commandOptions);
113+
114+
For more information on read preference options, see :manual:`Read
115+
Preference </core/read-preference/>` in the Server manual.
116+
117+
Response
118+
--------
119+
120+
Each method returns a ``Document`` object or a cursor that contains
121+
the response from the database after the command has been executed. Each
122+
database command performs a different function, so the response content
123+
can vary across commands. However, every response contains documents
124+
with the following fields:
125+
126+
.. list-table::
127+
:header-rows: 1
128+
:widths: 30 70
129+
130+
* - Field
131+
- Description
132+
133+
* - <command result>
134+
- Provides fields specific to the database command. For example,
135+
``count`` returns the ``n`` field and ``explain`` returns the
136+
``queryPlanner`` field.
137+
138+
* - ``ok``
139+
- Indicates whether the command has succeeded (``1``)
140+
or failed (``0``).
141+
142+
* - ``operationTime``
143+
- Indicates the logical time of the operation. MongoDB uses the
144+
logical time to order operations.
145+
To learn more about logical time, see our
146+
:website:`blog post about the Global Logical Clock </blog/post/transactions-background-part-4-the-global-logical-clock>`.
147+
148+
* - ``$clusterTime``
149+
- Provides a document that returns the signed cluster time. Cluster time is a
150+
logical time used for ordering of operations.
151+
152+
The document contains the following fields:
153+
154+
- ``clusterTime``, which is the timestamp of the highest known cluster time for the member.
155+
- ``signature``, which is a document that contains the hash of the cluster time and the ID
156+
of the key used to sign the cluster time.
157+
158+
Example
159+
-------
160+
161+
The following code shows how you can use the ``runCursorCommand()`` method to
162+
run the ``checkMetadataConsistency`` command on the ``testDB`` database
163+
and iterate through the results:
164+
165+
.. literalinclude:: /code-snippets/crud/runCommand.js
166+
:language: javascript
167+
:dedent:
168+
:start-after: start-runcommand
169+
:end-before: end-runcommand
170+
171+
Output
172+
~~~~~~
173+
174+
The output contains the contents of the cursor object. The documents
175+
describe any metadata inconsistencies in the database:
176+
177+
.. code-block:: javascript
178+
179+
{
180+
type: ...,
181+
description: ...,
182+
details: {
183+
namespace: ...,
184+
info: ...
185+
}
186+
}
187+
{
188+
type: ...,
189+
description: ...,
190+
details: {
191+
namespace: ...,
192+
collectionUUID: ...,
193+
maxKeyObj: ...,
194+
...
195+
}
196+
}
197+
198+
.. note::
199+
200+
If you store the command response in a cursor, you see only the
201+
command result documents when you access the contents of the cursor. You won't
202+
see the ``ok``, ``operationTime``, and ``$clusterTime`` fields.
203+
204+
.. _addl-info-runcommand:
205+
206+
Additional Information
207+
----------------------
208+
209+
For more information about the concepts in this guide, see the following documentation:
210+
211+
- :manual:`db.runCommand() </reference/method/db.runCommand/>`
212+
- :manual:`Database Commands </reference/command/>`
213+
- :manual:`hello Command </reference/command/hello/>`
214+
- :manual:`find Command </reference/command/find/>`

0 commit comments

Comments
 (0)