Skip to content

Commit adcb08f

Browse files
committed
Merge remote-tracking branch 'upstream/master' into v2.7
2 parents c6527bc + 0a6520f commit adcb08f

File tree

6 files changed

+225
-16
lines changed

6 files changed

+225
-16
lines changed

source/fundamentals.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Fundamentals
2222
/fundamentals/tracing-logging
2323
/fundamentals/run-command
2424
/fundamentals/performance
25+
/fundamentals/runtimes
2526

2627
..
2728
Connect to MongoDB Atlas from AWS Lambda <https://www.mongodb.com/docs/atlas/manage-connections-aws-lambda/>

source/fundamentals/run-command.txt

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ statistics, initializing a replica set, or running an aggregation pipeline.
2121
This guide includes the following sections:
2222

2323
- :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
2525

2626
- :ref:`Response <rust-command-response>` describes the information
27-
that the ``run_command()`` method returns
27+
that the command execution methods return
2828

2929
- :ref:`Command Example <rust-command-example>` provides a command
3030
example and describes the output for the command
@@ -51,8 +51,15 @@ Execute a Command
5151
-----------------
5252

5353
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.
5663

5764
The following code shows how you can use the ``run_command()``
5865
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:
6269

6370
let result = my_db.run_command(doc! { "hello": 1 }, None).await?;
6471

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+
6582
To find a link to a full list of database commands and corresponding
6683
parameters, see the :ref:`Additional Information section
6784
<rust-addtl-info-runcommand>`.
6885

6986
.. note:: Read Preference
7087

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.
7492

7593
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:
7997

8098
.. code-block:: rust
8199

82-
let command_options: SelectionCriteria = SelectionCriteria::ReadPreference(
100+
let command_options = SelectionCriteria::ReadPreference(
83101
ReadPreference::Primary
84102
);
85103
let result = my_db.run_command(command_doc, command_options).await?;
86104

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+
87109
For more information on read preference options, see :manual:`Read
88110
Preference </core/read-preference/>` in the Server manual.
89111

@@ -92,11 +114,14 @@ parameters, see the :ref:`Additional Information section
92114
Response
93115
--------
94116

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:
100125

101126
.. list-table::
102127
:header-rows: 1
@@ -214,5 +239,7 @@ API Documentation
214239
~~~~~~~~~~~~~~~~~
215240

216241
- `run_command() <{+api+}/struct.Database.html#method.run_command>`__
242+
- `run_cursor_command() <{+api+}/struct.Database.html#method.run_cursor_command>`__
217243
- `SelectionCriteria <{+api+}/options/enum.SelectionCriteria.html>`__
244+
- `RunCursorCommandOptions <{+api+}/options/struct.RunCursorCommandOptions.html>`__
218245
- `ReadPreference <{+api+}/options/enum.ReadPreference.html>`__

source/fundamentals/runtimes.txt

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
.. _rust-runtimes:
2+
3+
=================================
4+
Asynchronous and Synchronous APIs
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 about the {+driver-short+}'s :ref:`asynchronous <rust-runtimes-configure-async>`
17+
and :ref:`synchronous <rust-runtimes-configure-sync>` APIs.
18+
This guide explains how to enable the available APIs and structure your code to use each.
19+
20+
The {+driver-short+} supports ``tokio`` and ``async-std``, two popular asynchronous runtime crates.
21+
By default, the driver uses the ``tokio`` asynchronous runtime, but you can select a specific runtime
22+
by adding feature flags to the ``mongodb`` dependency in your ``Cargo.toml`` file.
23+
24+
The driver also includes a synchronous API for use cases that require blocking, or when parallelism is not necessary.
25+
You can select the synchronous API by adding feature flags to the ``mongodb`` dependency in your ``Cargo.toml`` file.
26+
27+
.. _rust-runtimes-configure-async:
28+
29+
Configure the Asynchronous Runtime
30+
----------------------------------
31+
32+
The driver uses the ``tokio`` runtime by default.
33+
You can explicitly choose a runtime by adding the ``"tokio-runtime"`` or ``"async-std-runtime"``
34+
feature flags to the ``mongodb`` dependency.
35+
36+
Select from the following tabs to see how to add feature flags for each corresponding crate:
37+
38+
.. tabs::
39+
40+
.. tab:: tokio
41+
:tabid: asynchronous-api-tokio
42+
43+
.. code-block:: toml
44+
45+
[dependencies.mongodb]
46+
version = "{+version+}"
47+
features = ["tokio-runtime"]
48+
49+
.. tab:: async-std
50+
:tabid: asynchronous-api-async-std
51+
52+
.. code-block:: toml
53+
54+
[dependencies.mongodb]
55+
version = "{+version+}"
56+
default-features = false
57+
features = ["async-std"]
58+
59+
For more information on installing the driver and adding feature flags,
60+
see the :ref:`rust-quick-start-download-and-install` step of the Quick Start.
61+
62+
Tokio Runtime Example
63+
~~~~~~~~~~~~~~~~~~~~~
64+
65+
The following code uses the ``task`` module from the ``tokio`` crate
66+
to create separate, concurrent tasks for multiple data operations:
67+
68+
.. literalinclude:: /includes/fundamentals/code-snippets/runtimes-tokio.rs
69+
:language: rust
70+
:dedent:
71+
72+
.. _rust-runtimes-configure-sync:
73+
74+
Configure the Synchronous API
75+
-----------------------------
76+
77+
The driver also provides a blocking, synchronous API.
78+
To use the synchronous API, add the either the ``"sync"`` or ``"tokio-sync"`` feature flag
79+
to the ``mongodb`` dependency.
80+
81+
Select from the following tabs to see how to add feature flags for each corresponding crate:
82+
83+
.. tabs::
84+
85+
.. tab:: sync
86+
:tabid: synchronous-api
87+
88+
.. code-block:: toml
89+
90+
[dependencies.mongodb]
91+
version = "{+version+}"
92+
default-features = false
93+
features = ["sync"]
94+
95+
.. tab:: tokio-sync
96+
:tabid: synchronous-api-tokio
97+
98+
.. code-block:: toml
99+
100+
[dependencies.mongodb]
101+
version = "{+version+}"
102+
default-features = false
103+
features = ["tokio-sync"]
104+
105+
Synchronous Code Example
106+
~~~~~~~~~~~~~~~~~~~~~~~~
107+
108+
When using the synchronous API, use types from the ``mongodb::sync`` module to perform operations.
109+
The following code uses the ``sync`` module to insert data into a collection using the synchronous API.
110+
When the ``insert_one`` method runs inside the ``for`` loop, the driver waits for each request to complete before continuing.
111+
112+
.. literalinclude:: /includes/fundamentals/code-snippets/runtimes-sync.rs
113+
:language: rust
114+
:dedent:
115+
116+
Use Both Asynchronous and Synchronous APIs
117+
------------------------------------------
118+
119+
You can use both asynchronous and synchronous APIs in the same application.
120+
For example, to enable both ``tokio`` runtimes, you can add the ``tokio`` dependency to your dependencies list, and add the
121+
``"tokio-sync"`` flag to the ``mongodb`` dependency:
122+
123+
.. code-block:: toml
124+
125+
[dependencies]
126+
futures = "0.3.28"
127+
tokio = {version = "1.32.0", features = ["full"]}
128+
129+
[dependencies.mongodb]
130+
version = "{+version+}"
131+
features = ["tokio-sync"]
132+
133+
Additional Information
134+
----------------------
135+
136+
For more information about the concepts in this guide, see the following pages:
137+
138+
- :ref:`Performance Considerations <rust-performance>`
139+
- `Asynchronous Programming in Rust <https://rust-lang.github.io/async-book/>`__
140+
141+
API Documentation
142+
~~~~~~~~~~~~~~~~~
143+
144+
To learn more about the methods and types discussed in this
145+
guide, see the following API Documentation:
146+
147+
- `Client <{+api+}/struct.Client.html>`__
148+
- `sync <{+api+}/sync/index.html>`__

source/includes/fundamentals-sections.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Fundamentals section:
1515
- :ref:`Record Driver Events <rust-tracing-logging>`
1616
- :ref:`Run A Database Command <rust-run-command>`
1717
- :ref:`Optimize Driver Performance <rust-performance>`
18+
- :ref:`Configure Asynchronous and Synchronous Runtimes <rust-runtimes>`
1819

1920
..
2021
- :atlas:`Connect to MongoDB Atlas from AWS Lambda </manage-connections-aws-lambda/>`
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use mongodb::sync::Client;
2+
3+
fn main() {
4+
let client = Client::with_uri_str("<connection string>")?;
5+
let some_data = doc! { "title": "1984", "author": "George Orwell" };
6+
7+
for i in 0..5 {
8+
let client_ref = client.clone();
9+
let somedata_ref = some_data.clone();
10+
11+
let collection = client_ref
12+
.database("items")
13+
.collection::<Document>(&format!("coll{}", i));
14+
15+
collection.insert_one(somedata_ref, None);
16+
}
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
let client = Client::with_uri_str("<connection string>").await?;
2+
let some_data = doc! { "title": "1984", "author": "George Orwell" };
3+
4+
for i in 0..5 {
5+
let client_ref = client.clone();
6+
let somedata_ref = some_data.clone();
7+
8+
task::spawn(async move {
9+
let collection = client_ref
10+
.database("items")
11+
.collection::<Document>(&format!("coll{}", i));
12+
13+
collection.insert_one(somedata_ref, None).await
14+
});
15+
}

0 commit comments

Comments
 (0)