Skip to content

Commit 6ff40e5

Browse files
committed
DOCSP-45854 Backport Skip Page (#164)
(cherry picked from commit 3904c6d)
1 parent 8983261 commit 6ff40e5

File tree

5 files changed

+233
-5
lines changed

5 files changed

+233
-5
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
Open Change Streams </fundamentals/crud/read-operations/change-streams>
1414
Search Text </fundamentals/crud/read-operations/text-search>
1515
Sort Data </fundamentals/crud/read-operations/sort>
16+
Skip Results </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

@@ -27,11 +27,12 @@ Read Operations
2727
- :ref:`rust-change-streams-guide`
2828
- :ref:`rust-search-text-guide`
2929
- :ref:`rust-sort-guide`
30+
- :ref:`rust-skip-guide`
3031

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

35-
.. - :ref:`rust-skip-guide`
36+
3637
.. - :ref:`rust-limit-guide`
3738
.. - :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
@@ -152,7 +152,8 @@ The following table describes commonly used settings that you can specify in
152152
| Type: ``ReadConcern``
153153

154154
* - ``skip``
155-
- | The number of documents to skip when returning results.
155+
- | The number of documents to skip when returning results. To learn more
156+
about how to use the ``skip()`` builder method, see :ref:`rust-skip-guide`.
156157
|
157158
| Type: ``u64``
158159
| Default: ``None``
@@ -394,8 +395,7 @@ following documentation:
394395
- :ref:`rust-cursor-guide` guide
395396
- :ref:`rust-aggregation` guide
396397
- :ref:`rust-sort-guide` guide
397-
398-
.. - :ref:`rust-skip-guide`
398+
- :ref:`rust-skip-guide` guide
399399

400400
.. - :ref:`rust-limit-guide`
401401
.. - :ref:`rust-project-guide`
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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/skip.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/skip.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+
If the number of skipped documents exceeds the number of matched documents for a
54+
query, then that query returns no documents.
55+
56+
Find operations return documents in a natural order that is not sorted on any
57+
fields. To avoid skipping random documents, use the ``sort()`` method to sort
58+
documents on a field with a unique value before setting a skip option. To learn
59+
more, see the :ref:`rust-sort-guide` guide.
60+
61+
.. _rust-skip-example:
62+
63+
Query Results Example
64+
~~~~~~~~~~~~~~~~~~~~~
65+
66+
To skip documents, you can initialize a ``FindOptions`` instance and specify the
67+
number of documents you want to skip using the ``skip()`` method. Then, pass
68+
your ``FindOptions`` struct as a parameter to the ``find()`` method.
69+
70+
This example runs a ``find()`` operation that performs the following actions:
71+
72+
- Sorts the results in ascending order of their ``author`` field values
73+
- Skips the first two documents
74+
- Returns the remaining documents
75+
76+
.. io-code-block::
77+
:copyable: true
78+
79+
.. input:: /includes/fundamentals/code-snippets/crud/skip.rs
80+
:start-after: start-skip-example
81+
:end-before: end-skip-example
82+
:language: rust
83+
:dedent:
84+
85+
.. output::
86+
:language: console
87+
:visible: false
88+
89+
Book { name: "A Dance with Dragons", author: "Martin", length: 1104 }
90+
Book { name: "Atlas Shrugged", author: "Rand", length: 1088 }
91+
92+
.. _rust-aggregation-skip:
93+
94+
Aggregation Example
95+
~~~~~~~~~~~~~~~~~~~
96+
97+
You can use the ``$skip`` stage in an aggregation pipeline to skip documents. To
98+
learn more about aggregation operations, see the :ref:`rust-aggregation` guide.
99+
100+
This example runs an aggregation pipeline that performs the following actions:
101+
102+
- Sorts the results in ascending order of their ``author`` field values
103+
- Skips the first document
104+
- Returns the remaining documents
105+
106+
.. io-code-block::
107+
:copyable: true
108+
109+
.. input:: /includes/fundamentals/code-snippets/crud/skip.rs
110+
:start-after: start-aggregation-example
111+
:end-before: end-aggregation-example
112+
:language: rust
113+
:dedent:
114+
115+
.. output::
116+
:language: console
117+
:visible: false
118+
119+
Document({"_id": ObjectId("..."), "name": String("Les Misérables"), "author": String("Hugo"), "length": Int32(1462)})
120+
Document({"_id": ObjectId("..."), "name": String("A Dance with Dragons"), "author": String("Martin"), "length": Int32(1104)})
121+
Document({"_id": ObjectId("..."), "name": String("Atlas Shrugged"), "author": String("Rand"), "length": Int32(1088)})
122+
123+
Additional Information
124+
----------------------
125+
126+
To learn more about the operations mentioned in this guide, see the following guides:
127+
128+
- :ref:`rust-query-guide`
129+
- :ref:`rust-retrieve-guide`
130+
- :ref:`rust-compound-operations`
131+
- :ref:`rust-aggregation`
132+
- :ref:`rust-sort-guide`
133+
134+
.. - :ref:`rust-limit-guide`
135+
136+
API Documentation
137+
~~~~~~~~~~~~~~~~~
138+
139+
To learn more about any of the methods or types discussed in this guide, see the
140+
following API documentation:
141+
142+
- `find() <{+api+}/struct.Collection.html#method.find>`__
143+
- `FindOptions <{+api+}/options/struct.FindOptions.html>`__
144+
- `FindOneOptions <{+api+}/options/struct.FindOneOptions.html>`__
145+
- `Cursor <{+api+}/struct.Cursor.html>`__
146+
- `aggregate() <{+api+}/struct.Collection.html#method.aggregate>`__
147+
- `AggregateOptions <{+api+}/options/struct.AggregateOptions.html>`__

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ To learn more about the operations mentioned in this guide, see the following:
174174
- :ref:`rust-retrieve-guide`
175175
- :ref:`rust-compound-operations`
176176
- :ref:`rust-aggregation`
177+
- :ref:`rust-skip-guide`
177178

178179
API Documentation
179180
~~~~~~~~~~~~~~~~~
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
use std::env;
2+
use mongodb::{ bson::doc, 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+
name: "The Brothers Karamazov".to_string(),
25+
author: "Dostoyevsky".to_string(),
26+
length: 824,
27+
},
28+
Book {
29+
name: "Atlas Shrugged".to_string(),
30+
author: "Rand".to_string(),
31+
length: 1088,
32+
},
33+
Book {
34+
name: "Les Misérables".to_string(),
35+
author: "Hugo".to_string(),
36+
length: 1462,
37+
},
38+
Book {
39+
name: "A Dance with Dragons".to_string(),
40+
author: "Martin".to_string(),
41+
length: 1104,
42+
},
43+
];
44+
45+
my_coll.insert_many(books, None).await?;
46+
// end-sample-data
47+
48+
// Retrieves documents in the collection, sorts results by their "author" field
49+
// values, and skips the first two results.
50+
// start-skip-example
51+
let find_options = FindOptions::builder()
52+
.sort(doc! { "author": 1 })
53+
.skip(2)
54+
.build();
55+
let mut cursor = my_coll.find(doc! {}, find_options).await?;
56+
57+
while let Some(result) = cursor.try_next().await? {
58+
println!("{:?}", result);
59+
}
60+
// end-skip-example
61+
62+
// Retrieves documents in the collection, sorts results by their "author" field,
63+
// then skips the first two results in an aggregation pipeline.
64+
// start-aggregation-example
65+
let pipeline = vec![
66+
doc! { "$match": {} },
67+
doc! { "$sort": { "author": 1 } }
68+
doc! { "$skip": 1 },
69+
];
70+
71+
let mut cursor = my_coll.aggregate(pipeline, None).await?;
72+
73+
while let Some(result) = cursor.try_next().await? {
74+
println!("{:?}", result);
75+
}
76+
// end-aggregation-example
77+
78+
Ok(())
79+
}

0 commit comments

Comments
 (0)