Queries
Sorting
Results come back in insertion order unless you sort them. Sorting, skipping and limiting are options of find.
Sort by a field
sort is a list of [field, direction] pairs. Use 1 for ascending and -1 for descending.
const youngestFirst = await users.find({}, { sort: [["age", 1]] }).toArray();
const newestFirst = await posts
.find({}, { sort: [["createdAt", -1]] })
.toArray();Sort by several fields
Later pairs break ties left by earlier ones:
await users
.find(
{},
{
sort: [
["country", 1],
["age", -1],
],
},
)
.toArray();This sorts by country, then by age from oldest to youngest within each country. Sorting is stable, so documents that tie on every field keep their insertion order.
Skip and limit
const pageSize = 20;
const page = 3;
const results = await posts
.find(
{ published: true },
{
sort: [["createdAt", -1]],
skip: (page - 1) * pageSize,
limit: pageSize,
},
)
.toArray();The order is always: filter, sort, skip, then limit. limit must be at least 1.
How values sort
A field can hold different types in different documents. Sorting orders types first, then values within a type. Ascending order, lowest first:
| Rank | Type |
|---|---|
| 1 | missing field |
| 2 | null |
| 3 | number and bigint |
| 4 | string |
| 5 | Uint8Array |
| 6 | CustomId |
| 7 | boolean |
| 8 | Date |
| 9 | array |
| 10 | object |
Descending is the exact reverse, so documents missing the field come last.
Within a type:
- Numbers and bigints sort together by value, so
1nfalls between0.5and1.5.NaNsorts first. - Strings sort by Unicode code point: uppercase before lowercase, and no locale rules.
falsesorts beforetrue.- Arrays all compare equal to each other, and so do objects. Sorting by an array field, or by a path through an array such as
"items.price", leaves those documents in their existing order.
Performance
Sorting is not served by indexes yet. The server finds the matching documents first and sorts them afterwards, so sorting a very large result takes time and memory. Narrow it down with a filter, or use limit.
The sort type is Sort, and the full rules are in query-semantics.md.