Skip to content

Commit 1d1f1a6

Browse files
committed
add page
1 parent 88dc621 commit 1d1f1a6

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed

source/index.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
Aggregation Operations </agg-exp-ops>
2323
Specialized Data Formats </data-formats>
2424
Builders </builders>
25+
Run a Command </run-command>
2526
Security </security>
2627
In-Use Encryption </encrypt-fields>
2728
Compatibility </compatibility>

source/run-command.txt

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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, specify the command and any relevant
37+
parameters in a document, then pass the document to the ``runCommand()`` method.
38+
39+
The following code shows how you can use the ``runCommand()``
40+
method to run the ``find`` command, which returns information about
41+
the current member's role in the replica set, on a database:
42+
43+
.. code-block:: javascript
44+
45+
val command = {
46+
find: "restaurants",
47+
filter: { rating: { $gte: 9 }, cuisine: "italian" },
48+
projection: { name: 1, rating: 1, address: 1 },
49+
sort: { name: 1 },
50+
limit: 5
51+
}
52+
val commandResult = database.runCommand(command)
53+
54+
For a full list of database commands and corresponding parameters, see
55+
the :manual:`Database Commands </reference/command/>` guide.
56+
57+
.. _kotlin-command-options:
58+
59+
Command Options
60+
---------------
61+
62+
You can specify optional command behavior for the ``runCommand()`` method by
63+
including a ``readPreference`` parameter. The following example shows how to
64+
specify a read preference and pass it as an option to the ``runCommand()``
65+
method:
66+
67+
.. code-block:: javascript
68+
69+
val command = { "hello": 1 }
70+
val commandReadPreference = { readPreference: "secondary" }
71+
72+
val commandResult = database.runCommand(command, readPreference)
73+
74+
For more information on read preference options, see :manual:`Read Preference
75+
</core/read-preference/>` in the Server manual.
76+
77+
.. note::
78+
79+
The ``command()`` method ignores the read preference setting you may have set
80+
on your ``database`` object. If no read preference is specified, this method
81+
uses the ``primary`` read preference.
82+
83+
Response
84+
--------
85+
86+
The ``runCommand()`` method returns a ``Document`` object that contains
87+
the response from the database after the command has been executed. Each
88+
database command performs a different function, so the response content
89+
can vary across commands. However, every response contains documents
90+
with the following fields:
91+
92+
.. list-table::
93+
:header-rows: 1
94+
:widths: 30 70
95+
96+
* - Field
97+
- Description
98+
99+
* - <command result>
100+
- Provides fields specific to the database command. For example,
101+
``count`` returns the ``n`` field and ``explain`` returns the
102+
``queryPlanner`` field.
103+
104+
* - ``ok``
105+
- Indicates whether the command has succeeded (``1``)
106+
or failed (``0``).
107+
108+
* - ``operationTime``
109+
- Indicates the logical time of the operation. MongoDB uses the
110+
logical time to order operations.
111+
To learn more about logical time, see our
112+
:website:`blog post about the Global Logical Clock </blog/post/transactions-background-part-4-the-global-logical-clock>`.
113+
114+
* - ``$clusterTime``
115+
- Provides a document that returns the signed cluster time. Cluster time is a
116+
logical time used for ordering of operations.
117+
118+
The document contains the following fields:
119+
120+
- ``clusterTime``, which is the timestamp of the highest known cluster time for the member.
121+
- ``signature``, which is a document that contains the hash of the cluster time and the ID
122+
of the key used to sign the cluster time.
123+
124+
Example
125+
-------
126+
127+
The following code shows how you can use the ``runCursorCommand()`` method to
128+
run the ``checkMetadataConsistency`` command on the ``testDB`` database
129+
and iterate through the results:
130+
131+
.. literalinclude:: /code-snippets/crud/runCommand.js
132+
:language: javascript
133+
:dedent:
134+
:start-after: start-runcommand
135+
:end-before: end-runcommand
136+
137+
Output
138+
~~~~~~
139+
140+
The output contains the contents of the cursor object. The documents
141+
describe any metadata inconsistencies in the database:
142+
143+
.. code-block:: javascript
144+
145+
{
146+
type: ...,
147+
description: ...,
148+
details: {
149+
namespace: ...,
150+
info: ...
151+
}
152+
}
153+
{
154+
type: ...,
155+
description: ...,
156+
details: {
157+
namespace: ...,
158+
collectionUUID: ...,
159+
maxKeyObj: ...,
160+
...
161+
}
162+
}
163+
164+
.. note::
165+
166+
If you store the command response in a cursor, you see only the
167+
command result documents when you access the contents of the cursor. You won't
168+
see the ``ok``, ``operationTime``, and ``$clusterTime`` fields.
169+
170+
.. _addl-info-runcommand:
171+
172+
Additional Information
173+
----------------------
174+
175+
For more information about the concepts in this guide, see the following documentation:
176+
177+
- Kotlin API
178+
- :driver:`runCommand() <{+api+}/apidocs/mongodb-driver-kotlin-sync/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-database/run-command.html>`
179+
- Database
180+
- :manual:`Database Commands </reference/command/>`
181+
- :manual:`hello Command </reference/command/hello/>`
182+
- :manual:`find Command </reference/command/find/>`

0 commit comments

Comments
 (0)