Limit the Number of Returned Results限制返回结果的数量¶
On this page
Overview概述¶
Use 使用limit
to cap the number of documents that can be returned from a read operation. limit
来限制读取操作可以返回的文档数。limit
functions as a cap on the maximum number of documents that the operation can return, but the operation can return a smaller number of documents if there are not enough documents present to reach the limit. limit
的作用是限制操作可以返回的最大文档数,但如果没有足够的文档达到限制,则操作可以返回较少的文档数。If 如果limit
is used with the skip method, the skip applies first and the limit only applies to the documents left over after the skip.limit
与skip
方法一起使用,则skip
将首先应用,而limit
仅适用于跳过后留下的文档。
Sample Documents样本文档¶
Follow the instructions in the examples below to insert data into a collection and return only certain results from a query using a sort, a skip, and a limit. 按照以下示例中的说明将数据插入集合,并使用排序、跳过和限制从查询中仅返回某些结果。Consider the following collection of documents that describe books:考虑以下描述书籍的文档集合:
[
{ "_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": "Tolkein", "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.要了解如何检查游标中存储的数据,请参阅游标基础知识页面。
Limit限制¶
The following example queries the collection to return the top three longest books. 下面的示例查询集合以返回最长的三本书。It matches all the documents with the query, applies a 它匹配查询中的所有文档,对sort
on the length
field to return books with longer lengths before books, and applies a limit
to return only 3
results:length
字段应用sort
以返回长度比图书长的图书,并应用limit
以仅返回3个结果:
// define an empty query document
const query = {};
// sort in descending (-1) order by length
const sort = { length: -1 };
const limit = 3;
const cursor = collection.find(query).sort(sort).limit(limit);
await cursor.forEach(console.dir);
The code example above outputs the following three documents, sorted by length:上面的代码示例输出以下三个文档,按长度排序:
{ "_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 }
The order in which you call 调用limit
and sort
does not matter because the driver reorders the calls to apply the sort first and the limit after it. limit
和sort
的顺序并不重要,因为驱动程序会对调用进行重新排序,以便首先应用sort
,然后应用limit
。The following two calls are equivalent:以下两个调用是等效的:
collection.find(query).sort({ length: -1 }).limit(3);
collection.find(query).limit(3).sort({ length: -1 });
You can also apply 您还可以通过在调用sort
and limit
by specifying them in an options
object in your call to the find()
method. The following two calls are equivalent:find()
方法的options
对象中指定排序和限制来应用它们。以下两个调用是等效的:
collection.find(query).sort({ length: -1 }).limit(3);
collection.find(query, { sort: { length: -1 }, limit: 3 });
For more information on the 有关options
settings for the find()
method, see the API documentation on find().find()
方法的options
设置的更多信息,请参阅find()
的API文档。
Skip¶
To see the next three books in the results, append the 要查看结果中的下三本书,请附加skip()
method, passing the number of documents to bypass as shown below:skip()
方法,传递要绕过的文档数,如下所示:
// define an empty query document
const query = {};
// sort in descending (-1) order by length
const sort = { length: -1 };
const limit = 3;
const skip = 3;
const cursor = collection.find(query).sort(sort).limit(limit).skip(skip);
await cursor.forEach(console.dir);
This operation returns the documents that describe the fourth through sixth books in order of longest-to-shortest length:此操作将按从最长到最短的顺序返回描述第四册到第六册的文档:
{ "_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 }
You can combine skip and limit in this way to implement paging for your collection, returning only small "slices" of the collection at once.您可以通过这种方式将skip和limit结合起来,为集合实现分页,一次只返回集合的小“片段”。