Limit & Skip
The limit
and skip
filters allow you to control the number of records returned by your query and to paginate through large datasets. These filters are particularly useful when working with large collections where you want to retrieve a specific subset of data.
Basic Usage
limit
: Specifies the maximum number of records to return.skip
: Specifies the number of records to skip before starting to return records.
Both filters accept a single numeric value.
Understanding the Content-Range Header
API response include a Content-Range
header. This header provides information about the range of records included in the response and the total number of records available.
Format of the Content-Range Header
The Content-Range
header is formatted as follows:
Content-Range: <type> <range-start>-<range-end>/<total>
<type>
: Indicates the type of resource, e.g.,documents
.<range-start>
and<range-end>
: Indicate the range of records returned in the current response (start at 0 and start/end are inclusive).<total>
: Indicates the total number of records in the full collection.
Examples
Limit
{
"limit": 10
}
This will return only the first 10
records of the query result.
Skip
{
"skip": 5
}
This will skip the first 5
records and return the subsequent records.
Combined Usage
{
"limit": 10,
"skip": 20
}
This will skip the first 20
records and then return the next 10
records. This combination is useful for paginating through results (e.g., page 3 of 10 records per page).
Notes
- The
limit
filter is typically used to control the size of the result set, which can improve performance when dealing with large datasets. - The
skip
filter is useful for implementing pagination, allowing you to retrieve a specific "page" of results. - When using both
limit
andskip
, theskip
is applied first, followed by thelimit
.
Conclusion
The limit
and skip
filters provide essential control over the size and offset of your query results. They are powerful tools for managing large datasets, enabling efficient data retrieval and pagination.