Sort Results排序结果

Use sort to change the order in which read operations return documents. 使用sort更改读取操作返回文档的顺序。Sort tells MongoDB to order returned documents by the values of one or more fields in a certain direction. sort告诉MongoDB按照一个或多个字段的值按特定方向对返回的文档进行排序。To sort returned documents by a field in ascending (lowest first) order, use a value of 1. 若要按字段升序(最低优先级)对返回的文档进行排序,请使用值1To sort in descending (greatest first) order instead, use -1. 要改为按降序(最大优先)排序,请使用-1If you do not specify a sort, MongoDB does not guarantee the order of query results.如果不指定排序,MongoDB不保证查询结果的顺序。

Follow the instructions in the examples below to insert data into a 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:要将此数据插入集合,请运行以下操作:

await collection.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.要了解如何检查游标中存储的数据,请参阅游标基础知识页面

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 = collection.find(query).sort(sort);
await cursor.forEach(console.dir);

In this case, the number -1 tells the read operation to sort the books in descending order by length. 在本例中,数字-1告诉read操作按长度降序对书籍进行排序。find() returns the following documents when this sort is used with an empty query:当此排序用于空查询时,find()返回以下文档:

{ "_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. 在上面的例子中,“A Dance with Dragons”和“Infinite Jest”都有1104页,因此它们返回的顺序不能保证。To resolve ties in your sorted results in a repeatable way, add additional 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 = collection.find(query).sort(sort);
await cursor.forEach(console.dir);

With the addition of the author field to the sort document, the read operation sorts matching documents first by length and, in the event of a tie, then by author. 在排序文档中添加author字段后,读取操作首先按length对匹配文档进行排序,如果是平局,则按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:当对匹配查询的文档使用此排序时,find()返回以下文档顺序,对长度相同的两本书将“Martin”排序在“Wallace”之前:

{ "_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 }