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 using the following methods:
limit()
: Specifies the maximum number of documents to return from a querysort()
: Specifies the sort order for the returned documentsskip()
: Specifies the number of documents to skip before returning query results
Sample Data
The examples in this guide use the sample_restaurants.restaurants
collection
from the Atlas sample datasets. To learn how to create a
free MongoDB Atlas cluster and load the sample datasets, see
Get Started.
Important
Project Reactor Library
This guide uses the Project Reactor library to consume Publisher
instances returned
by the Java Reactive Streams driver methods. To learn more about the Project Reactor library
and how to use it, see Getting Started
in the Reactor documentation. To learn more about how we use Project Reactor
library methods in this guide, see the Write Data to MongoDB guide.
Limit
To specify the maximum number of documents returned from a read operation,
chain the limit()
method to a find()
method and pass the number
of documents you want to return.
The following example finds all restaurants that have a cuisine
field value
of "Italian"
, and limits the results to 5
documents:
FindPublisher<Document> findPublisher = restaurants.find( eq("cuisine", "Italian")).limit(5); Flux.from(findPublisher) .doOnNext(x -> System.out.println(x.getString("name"))) .blockLast();
Isle Of Capri Resturant Italian Arturo'S Italian Patsy'S Italian Restaurant Italian Piccola Venezia Italian Roadhouse Restaurant Italian
Tip
The preceding example returns the first five documents returned by the query, in natural order. The following section describes how to return the documents in a specified sort order.
Sort
To return documents in a specified order, chain the sort()
method to the
find()
method. The sort()
method takes the sort direction
method as a parameter. To specify the sort direction, pass either the ascending()
or descending()
method to the sort()
method. Then, pass the field value you want to sort by.
The ascending()
method sorts values from lowest to highest, and
the descending()
method sorts them from highest to lowest. If you don't specify either
direction, the sort
method returns the results in ascending order.
The following example returns all documents with the cuisine
value of
"Italian"
, sorted by the name
field in ascending order:
FindPublisher<Document> findPublisher = restaurants.find( eq("cuisine", "Italian")).sort(ascending("name")); Flux.from(findPublisher) .doOnNext(x -> System.out.println(x.getString("name"))) .blockLast();
(Lewis Drug Store) Locanda Vini E Olii 101 Restaurant And Bar 44 Sw Ristorante & Bar 900 Park A Voce ... Zucchero E Pomodori
Skip
To skip a specified number of documents before returning your query results,
chain the skip()
method to the find()
method and pass the number of documents to skip. The
skip()
method ignores the specified number of documents in your query
results and returns the rest.
The following example returns all documents that have a borough
field value
of "Manhattan"
, and skips the first 10
documents:
FindPublisher<Document> findPublisher = restaurants.find( eq("borough", "Manhattan")).skip(10); Flux.from(findPublisher) .doOnNext(x -> System.out.println(x.getString("name"))) .blockLast();
Cafe Metro Lexler Deli Domino'S Pizza ...
Combine Limit, Sort, and Skip
You can combine the limit()
, sort()
, and skip()
methods in a single
operation by chaining them together. This allows you to set a maximum number of sorted documents to
return, 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 name
field value,
skipping the first 10
documents.
FindPublisher<Document> findPublisher = restaurants.find( eq("cuisine", "Italian")) .sort(ascending("name")) .limit(5) .skip(10); Flux.from(findPublisher) .doOnNext(x -> System.out.println(x.getString("name"))) .blockLast();
Acqua Acqua Restaurant Acqua Santa Acquista Trattoria Acquolina Catering
Note
The order in which you call these methods doesn't change the documents that are returned. The driver automatically reorders the calls to perform the sort and skip operations first, and the limit operation afterward.
Additional Information
For more information about specifying a query, see the Specify a Query guide.
For more information about retrieving documents, see the Retrieve Data guide.
API Documentation
To learn more about any of the methods or types discussed in this guide, see the following API documentation: