Skip to content

Commit 48731de

Browse files
authored
DOCSP-39852: Fluent API (#118)
1 parent 673e0b6 commit 48731de

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+547
-660
lines changed

source/faq.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ and using the ``try_next()`` method to iterate over ``Actor`` instances causes a
134134
// Add setup code here
135135

136136
let my_coll: Collection<Actor> = client.database("db").collection("actors");
137-
let mut cursor = my_coll.find(None, None).await?;
137+
let mut cursor = my_coll.find(doc! {}).await?;
138138
while let Some(result) = cursor.try_next().await? {
139139
println!("{:?}", result);
140140
};
@@ -195,15 +195,15 @@ using the ``?`` operator while handling an insert operation result:
195195
.. code-block:: rust
196196
:copyable: true
197197

198-
let insert_one_result = my_coll.insert_one(doc, None).await?;
198+
let insert_one_result = my_coll.insert_one(doc).await?;
199199

200200
Alternatively, you can create a conditional to handle the unwrapped values of ``InsertOneResult``.
201201
The following code uses the ``match`` keyword to process the ``insert_one()`` result:
202202

203203
.. code-block:: rust
204204
:copyable: true
205205

206-
let insert_one_result = my_coll.insert_one(doc, None).await;
206+
let insert_one_result = my_coll.insert_one(doc).await;
207207

208208
match insert_one_result {
209209
Ok(val) => {

source/fundamentals/collations.txt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ You can define a collation by specifying a collation locale and other options in
9696
struct instance. To begin building a ``Collation`` instance, call the ``Collation::builder()``
9797
method.
9898

99-
.. note:: Instantiating Options
99+
.. note:: Instantiating Structs
100100

101101
The {+driver-short+} implements the Builder design pattern for the
102-
creation of many different types, including ``Collation``. You can
102+
creation of some struct types, including ``Collation``. You can
103103
use the ``builder()`` method to construct an instance of each type
104104
by chaining option builder methods.
105105

@@ -181,9 +181,9 @@ Set a Collation on a Collection
181181
-------------------------------
182182

183183
When you create a new collection, you can define the collation for future operations
184-
called on that collection. Set the collation by using the ``collation()`` function
185-
when creating a ``CreateCollectionOptions`` instance. Then, call the ``create_collection()``
186-
method with your options instance as a parameter.
184+
called on that collection. Set the collation by chaining the ``collation()`` function
185+
to the ``create_collection()`` method, passing your ``Collation`` instance as a parameter
186+
to ``collation()``.
187187

188188
.. _rust-create-collection:
189189

@@ -308,9 +308,10 @@ This example performs the following actions:
308308

309309
- Sets the ``numeric_ordering`` collation option to ``true``, which ensures that values are sorted in
310310
numerical order rather than alphabetical order
311-
- Specifies a collation in a ``FindOptions`` instance, which overrides the collection's collation
312311
- Uses the ``find()`` method to return documents in which the value of the ``length`` field is greater
313312
than ``"1000"``
313+
- Specifies a collation by chaining the ``collation()`` method to the ``find()`` method, which overrides
314+
the collection's collation
314315

315316
.. io-code-block::
316317
:copyable: true

source/fundamentals/crud/compound-operations.txt

Lines changed: 39 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,11 @@ Modify Find and Delete Behavior
9797
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9898

9999
You can optionally modify the behavior of the ``find_one_and_delete()``
100-
method by passing a ``FineOneAndDeleteOptions`` instance as a parameter.
101-
To use default values for each setting, specify the value ``None`` for
102-
the options parameter.
100+
method by chaining option builder methods to ``find_one_and_delete()``. These
101+
builder methods set ``FindOneAndDeleteOptions`` struct fields.
103102

104103
The following table describes the options available in
105-
``FineOneAndDeleteOptions``:
104+
``FindOneAndDeleteOptions``:
106105

107106
.. list-table::
108107
:widths: 30 70
@@ -175,13 +174,15 @@ The following table describes the options available in
175174

176175
.. TODO add links to guides for relevant options
177176

178-
The {+driver-short+} implements the Builder design pattern for the
179-
creation of a ``FindOneAndDeleteOptions`` instance. You can use the
180-
type's ``builder()`` method to construct an options instance by
181-
chaining option builder functions one at a time.
177+
.. note:: Setting Options
178+
179+
You can set ``FindOneAndDeleteOptions`` fields by chaining option builder methods directly
180+
to the ``find_one_and_delete()`` method call. If you're using an earlier version of the driver,
181+
you must construct a ``FindOneAndDeleteOptions`` instance by chaining option builder methods
182+
to the ``builder()`` method. Then, pass your options instance as a parameter to ``find_one_and_delete()``.
182183

183-
The following code shows how to construct a ``FindOneAndDeleteOptions``
184-
instance and pass it to the ``find_one_and_delete()`` method:
184+
The following code shows how to set the ``comment`` field by chaining
185+
the ``comment()`` method to the ``find_one_and_delete()`` method:
185186

186187
.. literalinclude:: /includes/fundamentals/code-snippets/crud/compound.rs
187188
:start-after: begin-find-delete-options
@@ -236,12 +237,11 @@ Modify Find and Update Behavior
236237
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
237238

238239
You can optionally modify the behavior of the ``find_one_and_update()``
239-
method by passing a ``FindOneAndUpdateOptions`` instance as a parameter.
240-
To use default values for each setting, specify the value ``None`` for
241-
the options parameter.
240+
method by chaining option builder methods to ``find_one_and_update()``. These
241+
builder methods set ``FindOneAndUpdateOptions`` struct fields.
242242

243243
The following table describes the options available in
244-
``FineOneAndDeleteOptions``:
244+
``FindOneAndUpdateOptions``:
245245

246246
.. list-table::
247247
:widths: 30 70
@@ -343,13 +343,8 @@ The following table describes the options available in
343343
.. TODO add link to array updates page under option
344344
.. TODO add links to guides for relevant options
345345

346-
The {+driver-short+} implements the Builder design pattern for the
347-
creation of a ``FindOneAndUpdateOptions`` instance. You can use the
348-
type's ``builder()`` method to construct an options instance by
349-
chaining option builder methods one at a time.
350-
351-
The following code shows how to construct a ``FindOneAndUpdateOptions``
352-
instance and pass it to the ``find_one_and_update()`` method:
346+
The following code shows how to set the ``comment`` field by chaining
347+
the ``comment()`` method to the ``find_one_and_update()`` method:
353348

354349
.. literalinclude:: /includes/fundamentals/code-snippets/crud/compound.rs
355350
:start-after: begin-find-update-options
@@ -361,15 +356,15 @@ instance and pass it to the ``find_one_and_update()`` method:
361356
Find and Update Example
362357
~~~~~~~~~~~~~~~~~~~~~~~
363358

364-
This example shows how to call the ``find_one_and_update()`` method with the
365-
following parameters:
359+
This example shows how to perform the following actions:
366360

367-
- A query filter that matches a document where the value of ``school``
368-
is ``"Aurora High School"``
369-
- An update document that sets the ``school`` field to ``"Durango High School"``
370-
and increments the ``age`` field by ``1``
371-
- A ``FindOneAndUpdateOptions`` instance that returns the document
372-
*after* the update
361+
- Call the ``find_one_and_update()`` method
362+
- Pass a query filter to ``find_one_and_update()`` that matches a document where the
363+
value of ``school`` is ``"Aurora High School"``
364+
- Pass an update document to ``find_one_and_update()`` that sets the ``school`` field to
365+
``"Durango High School"`` and increments the ``age`` field by ``1``
366+
- Chain the ``return_document()`` method to ``find_one_and_update()`` to return
367+
the matched document *after* the update
373368

374369
.. io-code-block::
375370
:copyable: true
@@ -411,9 +406,8 @@ Modify Find and Replace Behavior
411406
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
412407

413408
You can optionally modify the behavior of the ``find_one_and_replace()``
414-
method by passing a ``FindOneAndReplaceOptions`` instance as a parameter.
415-
To use default values for each setting, specify the value ``None`` for
416-
the options parameter.
409+
method by chaining option builder methods to ``find_one_and_replace()``. These
410+
builder methods set ``FindOneAndReplaceOptions`` struct fields.
417411

418412
The following table describes the options available in
419413
``FindOneAndReplaceOptions``:
@@ -511,14 +505,9 @@ The following table describes the options available in
511505

512506
.. TODO add link to array updates page under option
513507
.. TODO add links to guides for relevant options
514-
515-
The {+driver-short+} implements the Builder design pattern for the
516-
creation of a ``FindOneAndReplaceOptions`` instance. You can use the
517-
type's ``builder()`` method to construct an options instance by
518-
chaining option builder functions one at a time.
519508

520-
The following code shows how to construct a ``FindOneAndReplaceOptions``
521-
instance and pass it to the ``find_one_and_replace()`` method:
509+
The following code shows how to set the ``comment`` field by chaining
510+
the ``comment()`` method to the ``find_one_and_replace()`` method:
522511

523512
.. literalinclude:: /includes/fundamentals/code-snippets/crud/compound.rs
524513
:start-after: begin-find-replace-options
@@ -530,15 +519,17 @@ instance and pass it to the ``find_one_and_replace()`` method:
530519
Find and Replace Example
531520
~~~~~~~~~~~~~~~~~~~~~~~~
532521

533-
This example shows how to call the ``find_one_and_replace()`` method with the
534-
following parameters:
535-
536-
- A query filter that matches a document where the value of ``name``
537-
includes the string ``"Johnson"``
538-
- A replacement document that describes a new student
539-
- A ``FindOneAndReplaceOptions`` instance that returns the document
540-
after replacement and projects only the ``name`` and ``school`` fields
541-
in the output
522+
This example performs the following actions:
523+
524+
- Calls the ``find_one_and_replace()`` method
525+
- Passes a query filter to ``find_one_and_replace()`` that matches a document
526+
where the value of ``name`` includes the string ``"Johnson"``
527+
- Passes a replacement document to ``find_one_and_replace()`` that describes a new
528+
student
529+
- Chains the ``return_document()`` method to ``find_one_and_replace()`` to return the document
530+
after replacement
531+
- Chains the ``projection()`` method to ``find_one_and_replace()``to project only the ``name``
532+
and ``school`` fields in the output
542533

543534
.. io-code-block::
544535
:copyable: true

source/fundamentals/crud/read-operations/change-streams.txt

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,9 @@ is ``"Wes Anderson"``, the preceding code produces the following output:
137137
Apply Aggregation Operators to your Change Stream
138138
-------------------------------------------------
139139

140-
You can pass an aggregation pipeline as a parameter to the ``watch()`` method
141-
to specify which change events the change stream receives.
140+
You can chain the ``pipeline()`` method to the ``watch()`` method
141+
to specify which change events the change stream receives. Pass an
142+
aggregation pipeline as a parameter to ``pipeline()``.
142143

143144
To learn which aggregation operators your MongoDB Server version supports, see
144145
:ref:`<change-stream-modify-output>` in the Server manual.
@@ -147,7 +148,7 @@ Example
147148
~~~~~~~
148149

149150
The following example creates an aggregation pipeline to filter for update
150-
operations. Then, the code passes the pipeline to the ``watch()`` method,
151+
operations. Then, the code passes the pipeline to the ``pipeline()`` method,
151152
configuring the change stream to only receive and print change events for
152153
update operations:
153154

@@ -222,22 +223,10 @@ must perform the following actions:
222223
Create a Collection with Pre-Image and Post-Images Enabled
223224
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
224225

225-
To create a collection with the pre-image and post-image documents enabled,
226-
configure this option in a ``CreateCollectionOptions`` struct instance. To begin
227-
building a ``CreateCollectionOptions`` instance, call the ``CreateCollectionOptions::builder()``
228-
method.
229-
230-
.. note:: Instantiating Options
231-
232-
The {+driver-short+} implements the Builder design pattern for the
233-
creation of many different types, including ``CreateCollectionOptions``. You
234-
can use the ``builder()`` method to construct an instance of each type
235-
by chaining option builder methods.
236-
237-
When configuring your ``CreateCollectionOptions`` instance, you must use the
238-
``change_stream_pre_and_post_images()`` builder method. The following example
239-
uses this builder method to specify collection options and creates a
240-
collection for which pre- and post-images are available:
226+
To enable pre-image and post-image documents for your collection, use the
227+
``change_stream_pre_and_post_images()`` option builder method. The following example
228+
uses this builder method to specify collection options and creates a collection
229+
for which pre- and post-images are available:
241230

242231
.. literalinclude:: /includes/fundamentals/code-snippets/crud/watch.rs
243232
:language: rust
@@ -262,11 +251,7 @@ Pre-Image Configuration Example
262251
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
263252

264253
To configure a change stream that returns change events containing the pre-image,
265-
specify a ``ChangeStreamOptions`` struct instance. To begin building a ``ChangeStreamOptions``
266-
instance, call the ``ChangeStreamOptions::builder()`` method.
267-
268-
When configuring your ``ChangeStreamOptions`` instance to return the pre-image document,
269-
you must use the ``full_document_before_change()`` builder method. The following example
254+
use the ``full_document_before_change()`` option builder method. The following example
270255
specifies change stream options and creates a change stream that returns pre-image
271256
documents:
272257

@@ -277,7 +262,7 @@ documents:
277262
:end-before: end-pre
278263

279264
The preceding example passes a value of ``FullDocumentBeforeChangeType::Required``
280-
to the ``full_document_before_change()`` builder method. This option configures the change
265+
to the ``full_document_before_change()`` option builder method. This method configures the change
281266
stream to require pre-images for replace, update, and delete change events. If the pre-image
282267
is not available, the driver raises an error.
283268

@@ -297,11 +282,7 @@ Post-Image Configuration Example
297282
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
298283

299284
To configure a change stream that returns change events containing the post-image,
300-
specify a ``ChangeStreamOptions`` struct instance. To begin building a ``ChangeStreamOptions``
301-
instance, call the ``ChangeStreamOptions::builder()`` method.
302-
303-
When configuring your ``ChangeStreamOptions`` instance to return the post-image document,
304-
you must use the ``full_document()`` builder method. The following example specifies change
285+
use the ``full_document()`` option builder method. The following example specifies change
305286
stream options and creates a change stream that returns post-image documents:
306287

307288
.. literalinclude:: /includes/fundamentals/code-snippets/crud/watch.rs
@@ -311,8 +292,8 @@ stream options and creates a change stream that returns post-image documents:
311292
:end-before: end-post
312293

313294
The preceding example passes a value of ``FullDocument::WhenAvailable`` to the ``full_document()``
314-
builder method. This option configures the change stream to return post-images for replace, update,
315-
and delete change events if the post-image is available.
295+
option builder method. This method configures the change stream to return post-images for replace,
296+
update, and delete change events if the post-image is available.
316297

317298
If you delete the document in which the value of ``name`` is ``"Todd Haynes"``, the
318299
change event produces the following output:

source/fundamentals/crud/read-operations/cursor.txt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -242,20 +242,20 @@ You can use the following methods to retrieve documents as an array:
242242
Specify Cursor Behavior
243243
-----------------------
244244

245-
To modify the cursor that an operation returns, pass options to
245+
To modify the cursor that an operation returns, chain option builder methods to
246246
the method that returns the ``Cursor`` instance. For example, you can
247-
specify cursor-related options in a ``FindOptions`` type that you pass to the
248-
``find()`` method.
247+
chain cursor-related option builder methods to the ``find()`` method.
249248

250-
.. note:: Instantiating Options
249+
.. note:: Setting Options
251250

252-
The {+driver-short+} implements the Builder design pattern for the
253-
creation of many different types, including ``FindOptions``. You can
254-
use each type's ``builder()`` method to construct an options instance
255-
by chaining option builder functions one at a time.
251+
You can set ``FindOptions`` fields by chaining option builder methods directly
252+
to the ``find()`` method call. If you're using an earlier version of the
253+
driver, you must construct a ``FindOptions`` instance by chaining option
254+
builder methods to the ``builder()`` method. Then, pass your ``FindOptions``
255+
instance as a parameter to ``find()``.
256256

257-
The following table describes cursor-related options that you can set in
258-
an options instance:
257+
The following table describes cursor-related options that you can set by
258+
calling their corresponding builder method:
259259

260260
.. list-table::
261261
:widths: 30 70
@@ -298,8 +298,8 @@ an options instance:
298298
| Type: ``bool``
299299
| Default: ``false``
300300

301-
The following code shows how to construct a ``FindOptions``
302-
instance and specify cursor-related settings:
301+
The following code shows how to specify cursor-related settings by chaining
302+
option builder methods to the ``find()`` method:
303303

304304
.. literalinclude:: /includes/fundamentals/code-snippets/crud/cursor.rs
305305
:start-after: start-options

0 commit comments

Comments
 (0)