Skip to content

Commit fddd491

Browse files
committed
version updates
1 parent d78003f commit fddd491

File tree

2 files changed

+15
-70
lines changed
  • source
    • fundamentals/crud/read-operations
    • includes/fundamentals/code-snippets/crud

2 files changed

+15
-70
lines changed

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

Lines changed: 10 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -60,53 +60,15 @@ Limit Documents
6060
You can specify the maximum number of documents to return in a query or in an
6161
aggregation pipeline.
6262

63-
This section describes how to limit results in the following ways:
64-
65-
- :ref:`limit() method <rust-limit-method>`: Chain the ``limit()`` method to the
66-
``find()`` method
67-
- :ref:`FindOptions struct <rust-findoptions-limit>`: Use the ``limit`` option
68-
- :ref:`Aggregation pipleline <rust-aggregation-limit>`: Create a pipeline that uses the ``$limit`` stage
69-
7063
.. _rust-limit-method:
7164

72-
limit() Method Example
73-
~~~~~~~~~~~~~~~~~~~~~~~
65+
Query Results Example
66+
~~~~~~~~~~~~~~~~~~~~~
7467

75-
To limit the number of documents returned, you can chain the ``limit()`` method
76-
to the ``find()`` method.
77-
78-
This example runs a ``find()`` operation that performs the following actions:
79-
80-
- Sorts the results in ascending order of their ``length`` field values
81-
- Limits the results to the first three documents
82-
83-
.. io-code-block::
84-
:copyable: true
85-
86-
.. input:: /includes/fundamentals/code-snippets/crud/limit.rs
87-
:start-after: start-limit-example
88-
:end-before: end-limit-example
89-
:language: rust
90-
:dedent:
91-
92-
.. output::
93-
:language: console
94-
:visible: false
95-
96-
Book { name: "The Brothers Karamazov", author: "Dostoyevsky", length: 824 }
97-
Book { name: "Atlas Shrugged", author: "Rand", length: 1088 }
98-
Book { name: "A Dance with Dragons", author: "Martin", length: 1104 }
99-
100-
.. _rust-findoptions-limit:
101-
102-
Options Example
103-
~~~~~~~~~~~~~~~
104-
105-
Alternatively, if you are setting and reusing options for your query, you can
106-
use ``FindOptions``. Set the ``limit`` field of the ``FindOptions`` struct by
107-
using the ``limit()`` option builder method. Then, chain the ``with_options()``
108-
method to the ``find()`` method and pass your ``FindOptions`` struct as a
109-
parameter to the ``with_options()`` method.
68+
To limit the number of documents returned, you can initialize a ``FindOptions``
69+
instance and specify the number of documents you want to limit using the
70+
``limit()`` method. Then, pass your ``FindOptions`` struct as a parameter to the
71+
``find()`` method.
11072

11173
This example runs a ``find()`` operation that performs the following actions:
11274

@@ -119,8 +81,8 @@ This example runs a ``find()`` operation that performs the following actions:
11981
:copyable: true
12082

12183
.. input:: /includes/fundamentals/code-snippets/crud/limit.rs
122-
:start-after: start-limit-options-example
123-
:end-before: end-limit-options-example
84+
:start-after: start-limit-example
85+
:end-before: end-limit-example
12486
:language: rust
12587
:dedent:
12688

@@ -158,8 +120,8 @@ This example runs an aggregation pipeline that performs the following actions:
158120
:language: console
159121
:visible: false
160122

161-
Document({"_id": Int32(3), "name": String("Les Misérables"), "author": String("Hugo"), "length": Int32(1462)})
162-
Document({"_id": Int32(4), "name": String("A Dance with Dragons"), "author": String("Martin"), "length": Int32(1104)})
123+
Document({"_id": ObjectId("..."), "name": String("Les Misérables"), "author": String("Hugo"), "length": Int32(1462)})
124+
Document({"_id": ObjectId("..."), "name": String("A Dance with Dragons"), "author": String("Martin"), "length": Int32(1104)})
163125

164126
Additional Information
165127
----------------------

source/includes/fundamentals/code-snippets/crud/limit.rs

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,64 +21,47 @@ async fn main() -> mongodb::error::Result<()> {
2121

2222
let books = vec![
2323
Book {
24-
id: 1,
2524
name: "The Brothers Karamazov".to_string(),
2625
author: "Dostoyevsky".to_string(),
2726
length: 824,
2827
},
2928
Book {
30-
id: 2,
3129
name: "Atlas Shrugged".to_string(),
3230
author: "Rand".to_string(),
3331
length: 1088,
3432
},
3533
Book {
36-
id: 3,
3734
name: "Les Misérables".to_string(),
3835
author: "Hugo".to_string(),
3936
length: 1462,
4037
},
4138
Book {
42-
id: 4,
4339
name: "A Dance with Dragons".to_string(),
4440
author: "Martin".to_string(),
4541
length: 1104,
4642
},
4743
];
4844

49-
my_coll.insert_many(books).await?;
45+
my_coll.insert_many(books, None).await?;
5046
// end-sample-data
5147

52-
// Retrieves documents in the collection, sorts results by their "length" field
53-
// values, and limits the results to three documents.
54-
// start-limit-example
55-
let mut cursor = my_coll
56-
.find(doc! {})
57-
.sort(doc! { "length": 1 })
58-
.limit(3).await?;
59-
60-
while let Some(result) = cursor.try_next().await? {
61-
println!("{:?}", result);
62-
}
63-
// end-limit-example
64-
6548
// Filters the results to only include documents where the "length" field value
6649
// is greater than 1000, then sorts results by their "length" field values, and
6750
// limits the results to the first two documents.
68-
// start-limit-options-example
51+
// start-limit-example
6952
let filter = doc! { "length": { "$gt": 1000 } };
7053

7154
let find_options = FindOptions::builder()
7255
.sort(doc! { "length": 1 })
7356
.limit(2)
7457
.build();
7558

76-
let mut cursor = my_coll.find(filter).with_options(find_options).await?;
59+
let mut cursor = my_coll.find(filter, find_options).await?;
7760

7861
while let Some(result) = cursor.try_next().await? {
7962
println!("{:?}", result);
8063
}
81-
// end-limit-options-example
64+
// end-limit-example
8265

8366
// Retrieves documents in the collection, sorts results by their "length" field
8467
// values, then limits the results to the first document.
@@ -89,7 +72,7 @@ let pipeline = vec![
8972
doc! { "$limit": 2 },
9073
];
9174

92-
let mut cursor = my_coll.aggregate(pipeline).await?;
75+
let mut cursor = my_coll.aggregate(pipeline, None).await?;
9376

9477
while let Some(result) = cursor.try_next().await? {
9578
println!("{:?}", result);

0 commit comments

Comments
 (0)