MongoDB sort, limit and skip
Ordering results and paginating them. Β· 9 practice questions Β· Cheat sheet β
A query returns a cursor, and you shape the result by chaining methods on it: .sort() orders, .limit() caps the count, .skip() drops leading documents. The order you call them in does not matter: MongoDB always sorts first, then skips, then limits. A multi-key sort like { cuisine: 1, name: -1 } orders by the first field and breaks ties with the second; 1 is ascending and -1 descending.
Skip and limit together give you MongoDB pagination: page three of ten rows is .skip(20).limit(10). It works, and every tutorial shows it, but skip still walks past the skipped documents, so deep pages get slower. For large collections the faster pattern is a range query on the sort key ("everything after the last _id I showed") with a limit.
MongoDB sort, limit and skip practice questions
- Return all restaurants sorted by
nameascending. easy restaurants - Return all restaurants sorted by
namedescending. easy restaurants - Sort restaurants by
cuisineascending, and within the same cuisine byboroughdescending. medium restaurants - Return the first 5 Manhattan restaurants ordered by
nameascending. medium restaurants - Return the next 5 Manhattan restaurants by
nameascending β that is, skip the first 5. medium restaurants - Return the 5 highest-rated movies. Show
titleandimdb.ratingonly, no_id. medium movies - Return the 3 cheapest products, showing
nameandpriceonly. medium products - How many restaurants are in
Brooklyn? medium restaurants - Return page 2 of all restaurants sorted by
seatsdescending, using a page size of 4. hard restaurants
Each question is answered in a live mongo-shell editor on this page; the result is checked against a reference solution.
sort(), limit() and skip() cheat sheet
Chain it on the cursor
db.restaurants.find({ borough: "Manhattan" })
.sort({ name: 1 }) // 1 = ascending, -1 = descending
.skip(5)
.limit(5)
Multi-key sort
.sort({ cuisine: 1, borough: -1 })
Key order matters: cuisine ascending first, then borough descending within
each cuisine.
Server-side order is fixed
No matter how you chain the methods, the server always applies:
sort β skip β limit
So .limit(5).sort(...) still sorts the whole result set first, then takes 5.
Pagination
// page N, 10 per page
.skip((N - 1) * 10).limit(10)
Large skip values get slow β real apps page by the last seen _id instead.
Gotchas
- Without
.sort()the order is not guaranteed, even if it looks stable. - Sorting on a missing field puts those documents first when ascending.
Common questions
How do I paginate results with skip and limit in MongoDB?
.skip((page - 1) * size).limit(size): db.restaurants.find().sort({ name: 1 }).skip(20).limit(10) is page three of ten. Always sort, or pages can overlap or skip rows between requests.Does the order of sort, skip and limit matter?
.limit(5).sort({ name: 1 }) still sorts the whole result before taking five. This differs from the aggregation pipeline, where stage order is literal.Why is skip slow on large collections?
{ _id: { $gt: lastId } } with a limit.Continue:β Pattern MatchingArray Queries β
MongoDB topics:Finding & ProjectionComparison OperatorsLogical OperatorsElement & TypePattern MatchingArray QueriesEmbedded DocumentsAggregation BasicsGrouping & Accumulators$unwind & Array PipelinesJoins & Advanced StagesFull cheat sheet