Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion docs/scripting/db/cursor/sort.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ cursor.sort(sort)
An object specifying the sort order, e.g. (( \{ n_hits: 1, n_fails: -1 \} )).
Each key is the path of a sort key, each value specifies direction (((%V1%)) for ascending, ((%V-1%)) for descending).

Note that the order of keys in the object matters.
Note that the order of properties in the object directly affects the sort order:

The sort object (( \{ n_hits: 1, n_fails: -1 \} )) sorts by ((%Nn_hits%)) ascending first, and only if two documents have identical ((%Nn_hits%)) values, sorts them by ((%Nn_fails%)) descending.
The sort object (( \{ n_fails: -1, n_hits: 1 \} )) does the opposite: it sorts by ((%Nn_fails%)) descending first, and only compares ((%Nn_hits%)) if ((%Nn_fails%)) on two documents is equal.

(In JavaScript, 'regular' properties are ordered by creation order, with the first created property appearing first. Keep this in mind when dynamically building sort objects.)

### Return

Expand Down
Loading