Sort Results对结果排序
On this page本页内容
Overview概述
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. 告诉MongoDB按照某个方向的一个或多个字段的值对返回的文档进行排序。To sort returned documents by a field in ascending (lowest first) order, use a value of 若要按字段升序(从低到低)对返回的文档进行排序,请使用值1
. 1
。To sort in descending (greatest first) order instead, use 若要按降序(先大后大)排序,请使用-1
. -1
。If you do not specify a sort, MongoDB does not guarantee the order of query results.如果不指定排序,MongoDB不会保证查询结果的顺序。
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. myDB.books
集合,并对查询结果执行排序。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 },
]);
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按长度降序(-1)排序
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. -1
告诉读取操作按长度降序对书籍进行排序。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. 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按长度升序(1)排序
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
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:当对匹配查询的文档使用此排序时,返回以下文档顺序,对于长度相同的两本书,将“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 }