Specify Documents to Return
On this page
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 examples in this guide use the restaurants
collection in the sample_restaurants
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.
The examples on this page use the following Restaurant
class as a model for the
documents in the collection:
[ ]public class Restaurant { public ObjectId Id { get; set; } [ ] public string Name { get; set; } [ ] public string Cuisine { get; set; } }
Limit
To specify the maximum number of documents returned from a read operation, use
the Limit()
method provided by the IFindFluent
interface. After calling
the Find()
method, chain the Limit()
method to modify the behavior of the
operation.
The following example finds all restaurants that have a cuisine
field value
of "Italian"
and limits the results to 5
documents:
var filter = Builders<Restaurant>.Filter.Eq("cuisine", "Italian"); var results = collection.Find(filter).Limit(5).ToList(); foreach (var result in results) { Console.WriteLine(result.Name); }
V & T Restaurant Mimis Restaurant & Bar Venice Restaurant Areo Restaurant Tre Giovani Pizza & Pasta
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, use the Sort()
method provided by
the IFindFluent
interface. After calling the Find()
method, chain the Sort()
method to modify the behavior of the operation.
When calling Sort()
, you must pass in the sort definition as a parameter. You can construct a sort
definition by using the Builders<T>.Sort.Ascending()
method to sort values from
lowest to highest, or the Builders<T>.Sort.Ascending()
method to sort them from highest
to lowest. Both of these methods take the field name to sort by as a parameter. These methods
can be chained to sort returned documents by multiple fields.
The following example returns all documents that have a cuisine
field value
of "Italian"
, sorted in ascending order of name
field values:
var filter = Builders<Restaurant>.Filter.Eq("cuisine", "Italian"); var sort = Builders<Restaurant>.Sort.Ascending("name"); var results = collection.Find(filter).Sort(sort).ToList(); foreach (var result in results) { Console.WriteLine(result.Name); }
(Lewis Drug Store) Locanda Vini E Olii 101 Restaurant And Bar 44 Sw Ristorante & Bar 900 Park A Voce ...
Skip
To skip a specified number of documents before returning your query results, use
the Skip()
method provided by the IFindFluent
interface. After calling
the Find()
method, chain the Skip()
method to modify the behavior of the
operation.
The following example returns all documents that have a cuisine
field value
of "Italian"
and skips the first 10
documents:
var filter = Builders<Restaurant>.Filter.Eq("cuisine", "Italian"); var results = collection.Find(filter).Skip(10).ToList(); foreach (var result in results) { Console.WriteLine(result.Name); }
Trattoria Alba Da Umberto Restaurant La Strada Restaurant Pasta Lovers Trattoria Nanni Restaurant Villa Mosconi Restaurant Villa Berulia Marco Polo Ristorante Cafe Luna Baraonda
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:
var filter = Builders<Restaurant>.Filter.Eq("cuisine", "Italian"); var sort = Builders<Restaurant>.Sort.Ascending("name"); var results = collection.Find(filter).Limit(10).Sort(sort).Skip(10).ToList(); foreach (var result in results) { Console.WriteLine(result.Name); }
Acqua Acqua Restaurant Acqua Santa Acquista Trattoria Acquolina Catering Adriatic Restaurant Pizzeria Bar Adrienne'S Pizza Bar Ai Fiori Aita Restaurant Al Di La
Note
The order in which you call these methods doesn't change the documents that are returned. The .NET/C# Driver 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 Retrieve Data guide.
For more information about specifying a query, see the Specify a Query guide.
API Documentation
To learn more about any of the methods or types discussed in this guide, see the following API documentation: