Overview
在本指南中,您可以学习;了解如何使用以下方法指定从读取操作中返回哪些文档:
sort():指定返回文档的排序顺序。limit():指定从查询中返回的最大文档数。skip():指定在返回查询结果之前要跳过的文档数。
示例样本数据
要运行本指南中的示例,请使用以下代码片段将描述图书的文档插入到 myDB.books集合中:
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 }, ]);
注意
在迭代游标之前,必须将游标方法(如 sort()、limit()、skip() 或 project())链接到读操作。如果在迭代游标后指定游标方法,则该设置不会应用读取操作。
注意
您的查询操作可能会返回对包含匹配文档的游标的引用。要学习;了解如何检查游标中存储的数据,请参阅从游标访问数据页面。
Sort
使用 sort() 方法更改读取操作返回文档的顺序。此方法指示MongoDB按一个或多个字段的值以某一方向对返回的文档进行排序。要按字段以升序(从低到高的顺序)对返回的文档进行排序,请使用值 1。要改为按降序(从高到低的顺序)排序,请使用 -1。如果不指定排序, MongoDB不保证查询结果的顺序。
以下示例将排序文档传递给读取操作,以确保该操作先返回长度较长的书籍,然后返回长度较短的书籍:
// define an empty query document const query = {}; // sort in descending (-1) order by length const sortFields = { length: -1 }; const cursor = myColl.find(query).sort(sortFields); for await (const doc of cursor) { console.dir(doc); }
在这种情况下,数字 -1 指示读取操作按长度对书籍进行降序排序。当此排序与空查询一起使用时,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 }
有时,使用指定的排序时,两个或多个文档的顺序是不明确的。在前面的示例中,title 值为 "A Dance with Dragons" 和 "Infinite Jest" 的文档的 length 都为 1104,因此不保证它们返回的顺序。要以可重复的方式解决排序结果中的“关列”问题,请在排序文档中添加更多字段:
// define an empty query document const query = {}; // sort in ascending (1) order by length const sortFields = { length: 1, author: 1 }; const cursor = myColl.find(query).sort(sortFields); for await (const doc of cursor) { console.dir(doc); }
在排序文档中添加 author字段后,读取操作首先按 length 对匹配的文档进行排序,如果存在并列项,则按 author 进行排序。匹配文档字段的比较顺序与排序文档中指定字段的顺序相同。对与查询匹配的文档使用此排序时,find() 返回以下文档顺序:
{ "_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 }
Limit
使用 limit() 方法限制读取操作可返回的文档数量。此方法指定操作可以返回的最大文档数,但如果存在的文档不足以达到此限制,则操作可能会返回较少数量的文档。如果将 limit() 与 skip() 方法一起使用,则首先应用 skip,并且限制仅应用于在 skip 操作后剩余的文档。
此示例将执行以下动作:
使用空查询过滤匹配集合中的所有文档
调用
sort()方法,对结果应用length字段的降序排序调用
limit()方法以仅返回前3个结果
// define an empty query document const query = {}; // sort in descending (-1) order by length const sortFields = { length: -1 }; const limitNum = 3; const cursor = myColl.find(query).sort(sortFields).limit(limitNum); for await (const doc of cursor) { console.dir(doc); }
上面的代码示例输出了以下三个文档,按图书长度排序:
{ "_id": 2, "title": "Les Misérables", "author": "Hugo", "length": 1462 } { "_id": 6, "title": "A Dance With Dragons", "author": "Martin", "length": 1104 } { "_id": 4, "title": "Infinite Jest", "author": "Wallace", "length": 1104 }
注意
调用 limit() 和 sort() 的顺序并不重要,因为驱动程序会对调用重新排序以首先应用排序。以下两个调用是等效的:
myColl.find(query).sort({ length: -1 }).limit(3); myColl.find(query).limit(3).sort({ length: -1 });
跳过
Use the skip() method to omit documents from the beginning of the read operation results. You can combine skip() with sort() to omit the top (for descending order) or bottom (for ascending order) results for a given query. Since the order of documents returned is not guaranteed in the absence of a sort, using skip() without using sort() omits arbitrary documents.
如果 skip() 的值超过了查询的匹配文档数,则该查询不会返回任何文档。
此示例通过执行以下操作来查询集合长度第五和第六高的书籍:
使用空查询过滤匹配集合中的所有文档
调用
sort()方法对length字段应用降序排序,这会先返回较长的图书,然后再返回较短的图书调用
skip()方法以省略结果中的前四个匹配文档
// define an empty query document const query = {}; const sortFields = { length: -1 }; const skipNum = 4; const cursor = myColl.find(query).sort(sortFields).skip(skipNum); for await (const doc of cursor) { console.dir(doc); }
由于该查询会跳过前四个匹配文档,因此前面的代码片段会打印第五和第六个最高长度的文档:
{ "_id": 5, "title": "Cryptonomicon", "author": "Stephenson", "length": 918 } { "_id": 1, "title": "The Brothers Karamazov", "author": "Dostoyevsky", "length": 824 }
组合限制、排序和跳过
您可以在单个操作中组合使用 limit、sort 和 skip 选项。 这允许您设立要返回的最大排序文档数,在返回之前跳过指定数量的文档。
以下示例返回 length 值为 "1104" 的文档。结果按字母顺序排序,跳过第一个文档,仅包含第一个结果:
const query = {length: "1104"}; const cursor = myColl.find(query).sort({ title: 1 }).skip(1).limit(1); for await (const doc of cursor) { console.dir(doc); }
注意
调用这些方法的顺序不会更改返回的文档。
更多信息
有关指定查询的更多信息,请参阅查询操作。
有关检索文档的更多信息,请参阅查找文档。
API 文档
要进一步了解本指南所讨论的任何方法,请参阅以下 API 文档: