Sort Results
On this page
Overview
Use sort
to change the order in which read operations return
documents. Sort
tells MongoDB to order returned documents by the
values of one or more fields in a certain direction. To sort returned
documents by a field in ascending (lowest first) order, use a value of
1
. To sort in descending (greatest first) order instead, use -1
.
If you do not specify a sort, MongoDB does not guarantee the order of
query results.
Sample Documents
Follow the instructions in the examples below to insert data into
the myDB.books
collection and perform a sort on the results of a query.
Consider a collection containing documents that describe books. To
insert this data into a collection, run the following operation:
const myDB = client.db("myDB"); const myColl = myDB.collection("books"); await myColl.insertMany([ { "_id": 1, "name": "The Brothers Karamazov", "author": "Dostoyevsky", "length": 824 }, { "_id": 2, "name": "Les Misérables", "author": "Hugo", "length": 1462 }, { "_id": 3, "name": "Atlas Shrugged", "author": "Rand", "length": 1088 }, { "_id": 4, "name": "Infinite Jest", "author": "Wallace", "length": 1104 }, { "_id": 5, "name": "Cryptonomicon", "author": "Stephenson", "length": 918 }, { "_id": 6, "name": "A Dance with Dragons", "author": "Martin", "length": 1104 }, ]);
Note
Your query operation may return a reference to a cursor that contains matching documents. To learn how to examine data stored in the cursor, see the Cursor Fundamentals page.
Example
Pass the following sort document to a read operation to ensure that the operation returns books with longer lengths before books with shorter lengths:
// define an empty query document const query = {}; // sort in descending (-1) order by length const sort = { length: -1 }; const cursor = myColl.find(query).sort(sort); for await (const doc of cursor) { console.dir(doc); }
In this case, the number -1
tells the read operation to sort the
books in descending order by length. find()
returns the following
documents when this sort is used with an empty query:
{ "_id": 2, "title": "Les Misérables", "author": "Hugo", "length": 1462 } { "_id": 4, "title": "Infinite Jest", "author": "Wallace", "length": 1104 } { "_id": 6, "title": "A Dance with Dragons", "author": "Martin", "length": 1104 } { "_id": 3, "title": "Atlas Shrugged", "author": "Rand", "length": 1088 } { "_id": 5, "title": "Cryptonomicon", "author": "Stephenson", "length": 918 } { "_id": 1, "title": "The Brothers Karamazov", "author": "Dostoyevsky", "length": 824 }
Sometimes, the order of two or more documents is ambiguous using a
specified sort. In the above case, both "A Dance with Dragons" and
"Infinite Jest" have 1104
pages, so the order in which they are
returned is not guaranteed. To resolve ties in your sorted results in a
repeatable way, add more fields to the sort document:
// define an empty query document const query = {}; // sort in ascending (1) order by length const sort = { length: 1, author: 1 }; const cursor = myColl.find(query).sort(sort); for await (const doc of cursor) { console.dir(doc); }
With the addition of the author
field to the sort document, the read operation sorts
matching documents first by length
then, if there is a tie, by author
. Matched
document fields are compared in the same order as fields are specified in the sort
document. find()
returns the following ordering of documents when this sort is used on
the documents matching the query, sorting "Martin"
before "Wallace"
for the two books with
the same length:
{ "_id": 1, "title": "The Brothers Karamazov", "author": "Dostoyevsky", "length": 824 } { "_id": 5, "title": "Cryptonomicon", "author": "Stephenson", "length": 918 } { "_id": 3, "title": "Atlas Shrugged", "author": "Rand", "length": 1088 } { "_id": 6, "title": "A Dance with Dragons", "author": "Martin", "length": 1104 } { "_id": 4, "title": "Infinite Jest", "author": "Wallace", "length": 1104 } { "_id": 2, "title": "Les Misérables", "author": "Hugo", "length": 1462 }