# Sorting and Paging: sort, limit and skip

sort puts results in order (1 up, -1 down) with a second field to break ties, limit caps how many come back, skip passes over the pages before, and countDocuments gives the number of pages.

Apprentice · MongoDB · Updated November 19, 2026

By Gregor Dzierzon

## After this lesson you can

- Sort results by one field or two with sort, using 1 for up and -1 for down
- Show one page of results with skip and limit, and work out the number of pages with countDocuments
- Choose the query a recipe page needs from dot notation, array matches, $elemMatch, projections, sort, skip and limit

## Key takeaways

- sort takes the fields to order by, 1 for up (smallest first) and -1 for down (largest first).
- Without a sort, results come back in no promised order, and documents that tie on the sort field can come back in any order. A second field breaks the tie.
- limit caps how many documents come back; skip passes over that many first.
- Page p, with n to a page, is skip of (p minus 1) times n, then limit n.
- The number of pages is countDocuments divided by the page size, rounded up with Math.ceil.

"Best rated first." "Quickest first." "Page 2 of 5." Nearly every list in an app comes in an order someone chose, a few at a time. A recipe site's top-rated list, a store's "price: low to high" and a search page's next button are all the same three tools, and you'll use them whenever a list is longer than a screen.

The examples use Sunday Kitchen's 40 recipes from *Fields Inside Fields: Dot Notation*.

## sort: 1 up, -1 down

A `find` gives back its documents in no promised order: here, the order they were stored in, which is nothing to rely on. Sunday Kitchen's dessert page wants the quickest dessert first. `sort` takes a document of the fields to order by, `1` for up (smallest first, read "up") and `-1` for down (largest first, read "down"), and goes after the `find`, joined by a dot:

```mongodb
db.recipes.find({ meal: "dessert" }, { name: 1, minutes: 1, _id: 0 }).sort({ minutes: 1 })
```

```text
[
  { name: 'Tiramisu', minutes: 30 },
  { name: 'Chocolate Chip Cookies', minutes: 30 },
  { name: 'Mango Sticky Rice', minutes: 40 },
  { name: 'Churros', minutes: 45 },
  { name: 'Lemon Bars', minutes: 50 },
  { name: 'Apple Crumble', minutes: 55 }
]
```

Text sorts A to Z with `1`, and Z to A with `-1`. A recipe with no rating, or `rating: null`, counts as smaller than every number, so it comes first going up and last going down.

## A tie needs a second field

Chocolate Chip Cookies and Tiramisu both take 30 minutes, and nothing says which comes first. Ties are common, and they matter most at the top of a list. The five best-rated recipes:

```mongodb
db.recipes.find({}, { name: 1, rating: 1, _id: 0 }).sort({ rating: -1 }).limit(5)
```

```text
[
  { name: 'Chocolate Chip Cookies', rating: 4.9 },
  { name: 'Chicken Tikka Masala', rating: 4.9 },
  { name: 'Beef Pho', rating: 4.8 },
  { name: 'Lemon Bars', rating: 4.8 },
  { name: 'Tiramisu', rating: 4.7 }
]
```

Three recipes are rated 4.7, and one of them comes fifth: which one isn't promised, so it can change from one run to the next. Give `sort` a second field, and it decides the ties. Here, ties are broken by name, A to Z:

```mongodb
db.recipes.find({}, { name: 1, rating: 1, _id: 0 }).sort({ rating: -1, name: 1 }).limit(5)
```

```text
[
  { name: 'Chicken Tikka Masala', rating: 4.9 },
  { name: 'Chocolate Chip Cookies', rating: 4.9 },
  { name: 'Beef Pho', rating: 4.8 },
  { name: 'Lemon Bars', rating: 4.8 },
  { name: 'Chickpea Curry', rating: 4.7 }
]
```

Chicken Tikka Masala now comes before Chocolate Chip Cookies, and Chickpea Curry takes fifth place, every time. Read `{ rating: -1, name: 1 }` as "rating down, then name up". The order of the fields matters: the first one decides, and the second only settles ties.

## limit and skip: one page at a time

The `limit(5)` above is the first tool for paging: `limit` caps how many documents come back. Its partner, `skip`, passes over that many first. Picture a stack of 40 cards dealt into piles of 8: the second pile is "skip 8, take the next 8". Sunday Kitchen's A to Z list, 8 recipes to a page, page 2:

```mongodb
db.recipes.find({}, { name: 1, _id: 0 }).sort({ name: 1 }).skip(8).limit(8)
```

```text
[
  { name: 'Chicken Tikka Masala' },
  { name: 'Chickpea Curry' },
  { name: 'Chocolate Chip Cookies' },
  { name: 'Churros' },
  { name: 'Falafel Wraps' },
  { name: 'French Toast' },
  { name: 'Greek Salad' },
  { name: 'Green Curry' }
]
```

For page `p`, with `n` recipes to a page, skip `(p - 1) * n` and limit `n`: page 1 skips 0, page 2 skips 8, page 3 skips 16. Always sort before you page. Without a sort, the order isn't promised, so a recipe could show up on two pages, or on none.

![Forty recipe cards, sorted A to Z, laid out in five rows of eight, labeled page 1 to page 5. The first row is grayed out and labeled skip 8. The second row, page 2, is highlighted and labeled limit 8, from Chicken Tikka Masala to Green Curry. The rows below are grayed out.](https://fsdtrainingmedia.blob.core.windows.net/public/images/2026/11/dc1fdf8b357c4c3ab08f8b3514d61786.webp)

## "Page 2 of 5": counting the pages

The "of 5" needs the number of recipes, which `countDocuments` gives, divided by the page size. A part-filled last page is still a page, so round up with `Math.ceil` (read "math ceiling": the next whole number up):

```mongodb
Math.ceil(db.recipes.countDocuments({}) / 8)
```

```text
5
```

The 27 vegetarian recipes, 8 to a page, need 4 pages, with only 3 on the last:

```mongodb
Math.ceil(db.recipes.countDocuments({ vegetarian: true }) / 8)
```

```text
4
```

Use the same filter for the count and for the page, or "page 2 of 5" will count recipes the page never shows.

## Why a big skip gets slow

`skip` doesn't jump to page 500: the server still finds and steps over every document before it, then throws them away. With 40 recipes you'll never notice. With millions of orders, page 50,000 makes the server walk past a lot of documents for each visit. *Why Queries Get Slow, and What an Index Is* in Module 4 comes back to this, and to sorting fast.

## Check yourself

2 questions to try on the lesson page.

## So what

A list is only useful in an order the reader expects, a page at a time. With `sort`, a tie-breaking second field, `skip`, `limit` and a page count, you can build the "best rated, page 2 of 5" list behind almost any app screen.

## Key terms

- [sort, limit and skip](https://www.ask-codey.com/glossary/sort-limit-and-skip)
- [countDocuments](https://www.ask-codey.com/glossary/countdocuments)
- [Cursor](https://www.ask-codey.com/glossary/cursor-mongodb)

## Related

- [Choosing Fields with Projections](https://www.ask-codey.com/lessons/mongodb-projections)
- [Finding Documents with find and findOne](https://www.ask-codey.com/lessons/mongodb-find-and-findone)

Source: https://www.ask-codey.com/lessons/mongodb-sorting-and-paging
