πŸƒDBQuestpractice MongoDB queries
0 / 110

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

  1. Return all restaurants sorted by name ascending. easy restaurants
  2. Return all restaurants sorted by name descending. easy restaurants
  3. Sort restaurants by cuisine ascending, and within the same cuisine by borough descending. medium restaurants
  4. Return the first 5 Manhattan restaurants ordered by name ascending. medium restaurants
  5. Return the next 5 Manhattan restaurants by name ascending – that is, skip the first 5. medium restaurants
  6. Return the 5 highest-rated movies. Show title and imdb.rating only, no _id. medium movies
  7. Return the 3 cheapest products, showing name and price only. medium products
  8. How many restaurants are in Brooklyn? medium restaurants
  9. Return page 2 of all restaurants sorted by seats descending, 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?

Sort on a stable key, then .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?

No. However you chain them, MongoDB applies sort, then skip, then limit. .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?

Skip has to scan and discard every skipped document, so page 1,000 reads a thousand pages' worth. Range-based pagination avoids it: remember the last sort value you showed and query { _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