@@ -21,10 +21,10 @@ statistics, initializing a replica set, or running an aggregation pipeline.
21
21
This guide includes the following sections:
22
22
23
23
- :ref:`Execute a Command <rust-execute-command>` describes the syntax
24
- and behavior of the ``run_command()`` method
24
+ and behavior of the ``run_command()`` and ``run_cursor_command()`` methods
25
25
26
26
- :ref:`Response <rust-command-response>` describes the information
27
- that the ``run_command()`` method returns
27
+ that the command execution methods return
28
28
29
29
- :ref:`Command Example <rust-command-example>` provides a command
30
30
example and describes the output for the command
@@ -51,8 +51,15 @@ Execute a Command
51
51
-----------------
52
52
53
53
To run a database command, you must specify the command and any relevant
54
- parameters in a command document, then pass the command document to the
55
- ``run_command()`` method.
54
+ parameters in a command document, then pass the command document to a
55
+ command execution method. The {+driver-short+} provides the following methods
56
+ to run database commands:
57
+
58
+ - ``run_command()``, which returns the command response as a
59
+ ``Document`` type. You can use this method with any database command.
60
+ - ``run_cursor_command()``, which returns the command response as an iterable
61
+ ``Cursor`` type. You can use this method only if your database command
62
+ returns multiple result documents.
56
63
57
64
The following code shows how you can use the ``run_command()``
58
65
method to run the ``hello`` command, which returns information about
@@ -62,28 +69,43 @@ the current member's role in the replica set, on a database:
62
69
63
70
let result = my_db.run_command(doc! { "hello": 1 }, None).await?;
64
71
72
+ The ``checkMetadataConsistency`` command returns multiple result
73
+ documents. You can use the ``run_cursor_command()`` method to run
74
+ this command and collect the results, as shown in the following code:
75
+
76
+ .. code-block:: rust
77
+
78
+ let cursor = my_db
79
+ .run_cursor_command(doc! { "checkMetadataConsistency": 1 }, None)
80
+ .await?;
81
+
65
82
To find a link to a full list of database commands and corresponding
66
83
parameters, see the :ref:`Additional Information section
67
84
<rust-addtl-info-runcommand>`.
68
85
69
86
.. note:: Read Preference
70
87
71
- The ``run_command()`` method does not obey the read preference you
72
- might have set on your ``Database`` object elsewhere in your code. By
73
- default, it uses the ``primary`` read preference.
88
+ The ``run_command()`` and ``run_cursor_command()`` methods do not
89
+ obey the read preference you might have set on your ``Database``
90
+ object elsewhere in your code. By default, they use the ``primary``
91
+ read preference.
74
92
75
93
You can set a read preference for command execution by
76
- passing an options object. The following code shows how to specify a
77
- read preference in a ``SelectionCriteria`` instance and pass it as an
78
- option to the ``run_command()`` method:
94
+ passing an options object to either method . The following code shows
95
+ how to specify a read preference in a ``SelectionCriteria`` instance
96
+ and pass it as an option to the ``run_command()`` method:
79
97
80
98
.. code-block:: rust
81
99
82
- let command_options: SelectionCriteria = SelectionCriteria::ReadPreference(
100
+ let command_options = SelectionCriteria::ReadPreference(
83
101
ReadPreference::Primary
84
102
);
85
103
let result = my_db.run_command(command_doc, command_options).await?;
86
104
105
+ The ``run_cursor_command()`` method takes a
106
+ ``RunCursorCommandOptions`` instance as a parameter. You can set the
107
+ ``selection_criteria`` field of this struct to select a read preference.
108
+
87
109
For more information on read preference options, see :manual:`Read
88
110
Preference </core/read-preference/>` in the Server manual.
89
111
@@ -92,11 +114,14 @@ parameters, see the :ref:`Additional Information section
92
114
Response
93
115
--------
94
116
95
- The ``run_command()`` method returns a ``Document`` instance that contains
96
- the response from the database after the command runs. Each
97
- database command performs a different function, so the response content
98
- can vary across commands. However, every response contains a document
99
- with the following fields:
117
+ The ``run_command()`` method returns a ``Document`` object that contains
118
+ the response from the database after the command has been executed. The
119
+ ``run_cursor_command()`` returns a ``Cursor`` that references multiple
120
+ result documents.
121
+
122
+ Each database command performs a different function, so the response
123
+ content can vary depending on the command executed. However, every
124
+ response contains a document with the following fields:
100
125
101
126
.. list-table::
102
127
:header-rows: 1
@@ -214,5 +239,7 @@ API Documentation
214
239
~~~~~~~~~~~~~~~~~
215
240
216
241
- `run_command() <{+api+}/struct.Database.html#method.run_command>`__
242
+ - `run_cursor_command() <{+api+}/struct.Database.html#method.run_cursor_command>`__
217
243
- `SelectionCriteria <{+api+}/options/enum.SelectionCriteria.html>`__
244
+ - `RunCursorCommandOptions <{+api+}/options/struct.RunCursorCommandOptions.html>`__
218
245
- `ReadPreference <{+api+}/options/enum.ReadPreference.html>`__
0 commit comments