Overview
In this guide, you can learn how to specify which documents to return
from a read operation by chaining the following methods to the find
method:
limit: Specifies the maximum number of documents to return from a query
sort: Specifies the sort order for the returned documents
skip: Specifies the number of documents to skip before returning query results
Sample Data
The example in this guide uses the movies collection in the sample_mflix
database from the Atlas sample datasets. To learn how to
create a free MongoDB Atlas cluster and load the sample datasets, see the
Get Started with Atlas guide.
Limit
To specify the maximum number of documents returned from a read operation, apply
the limit option to the operation. You can set this option by chaining the
limit method to the find method.
The following example finds all restaurants that have a cuisine field value
of 'Italian' and limits the results to 5 documents:
let mut cursor = my_coll .find(doc! { "cuisine": "Italian" }) .limit(5) .await?; while let Some(result) = cursor.try_next().await? { println!("{:?}", result); }
Restaurant { name: "V & T Restaurant", cuisine: "Italian" } Restaurant { name: "Gargiulo'S Restaurant", cuisine: "Italian" } Restaurant { name: "Patsy'S Italian Restaurant", cuisine: "Italian" } Restaurant { name: "John'S Restaurant", cuisine: "Italian" } Restaurant { name: "Ferdinando'S Restaurant", cuisine: "Italian" }
Tip
The preceding example returns the first five documents matched by the query according to their natural order in the database. The following section describes how to return the documents in a specified order.
Sort
To return documents in a specified order, apply the sort option to
the read operation. You can set this option by chaining the sort
setter method to the find method.
When calling sort, pass the field to sort the results by and the
sort direction. A sort direction value of 1 sorts values from lowest
to highest, and a value of -1 sorts them from highest to lowest. If you do not specify a sort, MongoDB does not guarantee the order of
query results.
The following example returns all documents that have a cuisine field value
of 'Italian', sorted in ascending order of name field values:
let mut cursor = my_coll .find(doc! { "cuisine": "Italian" }) .sort(doc! { "name": 1 }) .await?; while let Some(result) = cursor.try_next().await? { println!("{:?}", result); }
Restaurant { name: "Antonio'S Trattoria", cuisine: "Italian" } Restaurant { name: "Antonioni'S", cuisine: "Italian" } Restaurant { name: "Antonucci", cuisine: "Italian" } ... Restaurant { name: "Zucchero E Pomodori", cuisine: "Italian" }
Skip
To skip a specified number of documents before returning your query results, apply
the skip option to the read operation. You can set this option by chaining the
skip setter method to the find method.
The following example returns all documents that have a borough field value
of 'Manhattan' and skips the first 10 documents:
let mut cursor = my_coll .find(doc! { "borough": "Manhattan" }) .skip(10) .await?; while let Some(result) = cursor.try_next().await? { println!("{:?}", result); }
Restaurant { name: "Beyond Thai Kitchen", cuisine: "Thai" } Restaurant { name: "Rainbow Room", cuisine: "American" } ... Restaurant { name: "Fairfield Inn Suites Penn Station", cuisine: "Other" }
Combine Limit, Sort, and Skip
You can chain the limit, sort, and skip methods to a single
find method call. This allows you to set a maximum number of sorted documents
to return from the read operation, skipping a specified number of documents before
returning.
The following example returns 5 documents that have a cuisine value of
'Italian'. The results are sorted in ascending order by the name field value,
skipping the first 10 documents:
let mut cursor = my_coll .find(doc! { "cuisine": "Italian" }) .sort(doc! { "name": 1 }) .skip(10) .limit(5) .await?; while let Some(result) = cursor.try_next().await? { println!("{:?}", result); }
Restaurant { name: "Acqua", cuisine: "Italian" } Restaurant { name: "Acqua Restaurant", cuisine: "Italian" } Restaurant { name: "Acqua Santa", cuisine: "Italian" } Restaurant { name: "Acquista Trattoria", cuisine: "Italian" } Restaurant { name: "Acquolina Catering", cuisine: "Italian" }
Note
The order in which you call these methods doesn't change the documents that are returned. The server automatically reorders the calls to perform the sort operation first, the skip operation next, and then the limit operation.
Additional Information
For more information about retrieving documents, see the Find Documents guide.
For more information about specifying a query, see the Specify a Query guide.
API Documentation
To learn more about the find method and its options, see the
API documentation.