Skip to content

Commit a0b4203

Browse files
committed
DOCSP-43424 Skip returned results (#133)
(cherry picked from commit cb8a477)
1 parent 7d614d2 commit a0b4203

File tree

6 files changed

+294
-8
lines changed

6 files changed

+294
-8
lines changed

source/fundamentals/crud/read-operations.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ Read Operations
1313
/fundamentals/crud/read-operations/change-streams
1414
/fundamentals/crud/read-operations/text-search
1515
/fundamentals/crud/read-operations/sort
16+
/fundamentals/crud/read-operations/skip
1617

1718
..
1819
/fundamentals/crud/read-operations/count
1920
/fundamentals/crud/read-operations/distinct
20-
/fundamentals/crud/read-operations/skip
2121
/fundamentals/crud/read-operations/limit
2222
/fundamentals/crud/read-operations/project
2323

@@ -28,11 +28,12 @@ Read Operations
2828
- :ref:`rust-change-streams-guide`
2929
- :ref:`rust-search-text-guide`
3030
- :ref:`rust-sort-guide`
31+
- :ref:`rust-skip-guide`
3132

3233
.. - :ref:`rust-query-guide`
3334
.. - :ref:`rust-count-guide`
3435
.. - :ref:`rust-distinct-guide`
3536

36-
.. - :ref:`rust-skip-guide`
37+
3738
.. - :ref:`rust-limit-guide`
3839
.. - :ref:`rust-project-guide`

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ fields that you can set by calling their corresponding builder methods:
149149
| Type: ``ReadConcern``
150150

151151
* - ``skip``
152-
- | The number of documents to skip when returning results.
152+
- | The number of documents to skip when returning results. To learn more
153+
about how to use the ``skip()`` builder method, see :ref:`rust-skip-guide`.
153154
|
154155
| Type: ``u64``
155156
| Default: ``None``
@@ -388,8 +389,7 @@ following documentation:
388389
- :ref:`rust-cursor-guide` guide
389390
- :ref:`rust-aggregation` guide
390391
- :ref:`rust-sort-guide` guide
391-
392-
.. - :ref:`rust-skip-guide`
392+
- :ref:`rust-skip-guide` guide
393393

394394
.. - :ref:`rust-limit-guide`
395395
.. - :ref:`rust-project-guide`
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
.. _rust-skip-guide:
2+
3+
=====================
4+
Skip Returned Results
5+
=====================
6+
7+
.. facet::
8+
:name: genre
9+
:values: reference
10+
11+
.. meta::
12+
:keywords: code example, read operation, skip, skip results
13+
14+
.. contents:: On this page
15+
:local:
16+
:backlinks: none
17+
:depth: 2
18+
:class: singlecol
19+
20+
Overview
21+
--------
22+
23+
In this guide, you can learn how to use the {+driver-long+} to perform a read
24+
operation that skips a specified number of documents when returning results.
25+
26+
Sample Data for Examples
27+
------------------------
28+
29+
The examples in this guide use the following ``Book`` struct as a model for
30+
documents in the ``books`` collection:
31+
32+
.. literalinclude:: /includes/fundamentals/code-snippets/crud/sort.rs
33+
:start-after: start-book-struct
34+
:end-before: end-book-struct
35+
:language: rust
36+
:dedent:
37+
38+
The following code shows how to insert sample data into the ``books``
39+
collection:
40+
41+
.. literalinclude:: /includes/fundamentals/code-snippets/crud/sort.rs
42+
:start-after: start-sample-data
43+
:end-before: end-sample-data
44+
:language: rust
45+
:dedent:
46+
47+
Skip Documents
48+
--------------
49+
50+
You can skip results retrieved by a query, or you can skip results within an
51+
aggregation pipeline.
52+
53+
This section describes how to skip results in the following ways:
54+
55+
- :ref:`skip() method <rust-skip-method>`: Chain the ``skip()`` method to the
56+
``find()`` method
57+
- :ref:`FindOptions struct <rust-findoptions-skip>`: Use the ``skip()`` option
58+
builder method to configure a ``FindOptions`` struct
59+
- :ref:`Aggregation pipleline <rust-aggregation-skip>`: Create a pipeline that uses the ``$skip`` stage
60+
61+
If the number of skipped documents exceeds the number of matched documents for a
62+
query, then that query returns no documents.
63+
64+
Find operations return documents in a natural order that is not sorted on any
65+
fields. To avoid skipping random documents, use the ``sort()`` method to sort
66+
documents on a field with a unique value before setting a skip option. To learn
67+
more, see the :ref:`rust-sort-guide` guide.
68+
69+
.. _rust-skip-method:
70+
71+
skip() Method Example
72+
~~~~~~~~~~~~~~~~~~~~~~
73+
74+
To skip documents, you can chain the ``skip()`` method to the ``find()`` method.
75+
The ``skip()`` method takes an integer that specifies the number of documents to
76+
omit from the beginning of the result set.
77+
78+
This example runs a ``find()`` operation that performs the following actions:
79+
80+
- Sorts the results in ascending order of their ``author`` field values
81+
- Skips the first two documents
82+
- Returns the remaining documents
83+
84+
.. io-code-block::
85+
:copyable: true
86+
87+
.. input:: /includes/fundamentals/code-snippets/crud/skip.rs
88+
:start-after: start-skip-example
89+
:end-before: end-skip-example
90+
:language: rust
91+
:dedent:
92+
93+
.. output::
94+
:language: console
95+
:visible: false
96+
97+
Book { name: "A Dance with Dragons", author: "Martin", length: 1104 }
98+
Book { name: "Atlas Shrugged", author: "Rand", length: 1088 }
99+
100+
.. _rust-findoptions-skip:
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 ``skip`` field of the ``FindOptions`` struct by
107+
using the ``skip()`` 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.
110+
111+
This example runs a ``find()`` operation that performs the following actions:
112+
113+
- Sorts the results in descending order of their ``name`` field values
114+
- Skips the first document
115+
- Returns the remaining documents
116+
117+
.. io-code-block::
118+
:copyable: true
119+
120+
.. input:: /includes/fundamentals/code-snippets/crud/skip.rs
121+
:start-after: start-options-skip-example
122+
:end-before: end-options-skip-example
123+
:language: rust
124+
:dedent:
125+
126+
.. output::
127+
:language: console
128+
:visible: false
129+
130+
Book { name: "Les Misérables", author: "Hugo", length: 1462 }
131+
Book { name: "Atlas Shrugged", author: "Rand", length: 1088 }
132+
133+
.. _rust-aggregation-skip:
134+
135+
Aggregation Example
136+
~~~~~~~~~~~~~~~~~~~
137+
138+
You can use the ``$skip`` stage in an aggregation pipeline to skip documents. To
139+
learn more about aggregation operations, see the :ref:`rust-aggregation` guide.
140+
141+
This example runs an aggregation pipeline that performs the following actions:
142+
143+
- Sorts the results in ascending order of their ``author`` field values
144+
- Skips the first document
145+
- Returns the remaining documents
146+
147+
.. io-code-block::
148+
:copyable: true
149+
150+
.. input:: /includes/fundamentals/code-snippets/crud/skip.rs
151+
:start-after: start-aggregation-example
152+
:end-before: end-aggregation-example
153+
:language: rust
154+
:dedent:
155+
156+
.. output::
157+
:language: console
158+
:visible: false
159+
160+
Document({"_id": Int32(3), "name": String("Les Misérables"), "author": String("Hugo"), "length": Int32(1462)})
161+
Document({"_id": Int32(4), "name": String("A Dance with Dragons"), "author": String("Martin"), "length": Int32(1104)})
162+
Document({"_id": Int32(2), "name": String("Atlas Shrugged"), "author": String("Rand"), "length": Int32(1088)})
163+
164+
Additional Information
165+
----------------------
166+
167+
To learn more about the operations mentioned in this guide, see the following guides:
168+
169+
- :ref:`rust-query-guide`
170+
- :ref:`rust-retrieve-guide`
171+
- :ref:`rust-compound-operations`
172+
- :ref:`rust-aggregation`
173+
- :ref:`rust-sort-guide`
174+
.. - :ref:`rust-limit-guide`
175+
176+
API Documentation
177+
~~~~~~~~~~~~~~~~~
178+
179+
To learn more about any of the methods or types discussed in this guide, see the
180+
following API documentation:
181+
182+
- `find() <{+api+}/struct.Collection.html#method.find>`__
183+
- `FindOptions <{+api+}/options/struct.FindOptions.html>`__
184+
- `FindOneOptions <{+api+}/options/struct.FindOneOptions.html>`__
185+
- `Cursor <{+api+}/struct.Cursor.html>`__
186+
- `aggregate() <{+api+}/struct.Collection.html#method.aggregate>`__
187+
- `AggregateOptions <{+api+}/options/struct.AggregateOptions.html>`__

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,18 +182,19 @@ The following example specifies a descending sort on the ``name`` field:
182182
Additional Information
183183
----------------------
184184

185-
To learn more about the operations metnioned in this guide, see the following:
185+
To learn more about the operations mentioned in this guide, see the following:
186186

187187
- :ref:`rust-query-guide`
188188
- :ref:`rust-retrieve-guide`
189189
- :ref:`rust-compound-operations`
190190
- :ref:`rust-aggregation`
191+
- :ref:`rust-skip-guide`
191192

192193
API Documentation
193194
~~~~~~~~~~~~~~~~~
194195

195196
To learn more about any of the methods or types discussed in this guide, see the
196-
following API Documentation:
197+
following API documentation:
197198

198199
- `find() <{+api+}/struct.Collection.html#method.find>`__
199200
- `FindOptions <{+api+}/options/struct.FindOptions.html>`__
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
use std::env;
2+
use mongodb::{ bson::doc, bson::Document, Client, Collection, options::FindOptions };
3+
use serde::{Deserialize, Serialize};
4+
use futures::stream::TryStreamExt;
5+
6+
// start-book-struct
7+
#[derive(Debug, Serialize, Deserialize)]
8+
struct Book {
9+
name: String,
10+
author: String,
11+
length: i32,
12+
}
13+
// end-book-struct
14+
15+
#[tokio::main]
16+
async fn main() -> mongodb::error::Result<()> {
17+
// start-sample-data
18+
let uri = "connection string";
19+
let client = Client::with_uri_str(uri).await?;
20+
let my_coll: Collection<Book> = client.database("db").collection("books");
21+
22+
let books = vec![
23+
Book {
24+
id: 1,
25+
name: "The Brothers Karamazov".to_string(),
26+
author: "Dostoyevsky".to_string(),
27+
length: 824,
28+
},
29+
Book {
30+
id: 2,
31+
name: "Atlas Shrugged".to_string(),
32+
author: "Rand".to_string(),
33+
length: 1088,
34+
},
35+
Book {
36+
id: 3,
37+
name: "Les Misérables".to_string(),
38+
author: "Hugo".to_string(),
39+
length: 1462,
40+
},
41+
Book {
42+
id: 4,
43+
name: "A Dance with Dragons".to_string(),
44+
author: "Martin".to_string(),
45+
length: 1104,
46+
},
47+
];
48+
49+
my_coll.insert_many(books).await?;
50+
// end-sample-data
51+
52+
// Retrieves documents in the collection, sorts results by their "author" field
53+
// values, and skips the first two results.
54+
// start-skip-example
55+
let mut cursor = my_coll
56+
.find(doc! {})
57+
.sort(doc! { "author": 1 })
58+
.skip(2).await?;
59+
60+
while let Some(result) = cursor.try_next().await? {
61+
println!("{:?}", result);
62+
}
63+
// end-skip-example
64+
65+
// Sets the values for the `FindOptions` struct to sort results by their "name"
66+
// field values, skip the first two results, and return the remaining results.
67+
// start-options-skip-example
68+
let find_options = FindOptions::builder()
69+
.sort(doc! { "name": -1 })
70+
.skip(1)
71+
.build();
72+
73+
let mut cursor = my_coll.find(doc! {}).with_options(find_options).await?;
74+
75+
while let Some(result) = cursor.try_next().await? {
76+
println!("{:?}", result);
77+
}
78+
// end-options-skip-example
79+
80+
// Retrieves documents in the collection, sorts results by their "author" field,
81+
// then skips the first two results in an aggregation pipeline.
82+
// start-aggregation-example
83+
let pipeline = vec![
84+
doc! { "$match": {} },
85+
doc! { "$sort": { "author": 1 } }
86+
doc! { "$skip": 1 },
87+
];
88+
89+
let mut cursor = my_coll.aggregate(pipeline).await?;
90+
91+
while let Some(result) = cursor.try_next().await? {
92+
println!("{:?}", result);
93+
}
94+
// end-aggregation-example
95+
96+
Ok(())
97+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ while let Some(result) = cursor.try_next().await? {
7777

7878
// start-sort-aggregation
7979
let pipeline = vec![
80-
docs! { "$match": {} },
80+
doc! { "$match": {} },
8181
// 1 for ascending order, -1 for descending order
8282
doc! { "$sort": { "author": 1 } }
8383
];

0 commit comments

Comments
 (0)