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. 作用是限制操作可以返回的最大文档数,但如果没有足够的文档来达到限制,则操作可以返回较少的文档数。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
将首先应用,并且限制仅适用于跳过后剩余的文档。
Sample Documents示例文档
To follow along with the examples in this guide, use the following code snippet to insert documents that describe books into the 要遵循本指南中的示例,请使用以下代码片段将描述书籍的文档插入myDB.books
collection: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 },
]);
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
以返回书籍之前长度较长的书籍,并应用限制仅返回3个结果:
//define an empty query document定义一个空的查询文档
const query = {};
//sort in descending (-1) order by length按长度降序(-1)排序
const sort = { length: -1 };
const limit = 3;
const cursor = myColl.find(query).sort(sort).limit(limit);
for await (const doc of cursor) {
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
的顺序并不重要,因为驱动程序会对调用进行重新排序,以便首先应用排序,然后应用limit
。The following two calls are equivalent:以下两个调用是等效的:
myColl.find(query).sort({ length: -1 }).limit(3);
myColl.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. find()
方法的调用中的options
对象中指定sort
和limit
来应用它们。The following two calls are equivalent:以下两个调用是等效的:
myColl.find(query).sort({ length: -1 }).limit(3);
myColl.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按长度降序(-1)排序
const sort = { length: -1 };
const limit = 3;
const skip = 3;
const cursor = myColl.find(query).sort(sort).limit(limit).skip(skip);
for await (const doc of cursor) {
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
结合起来,为集合实现分页,一次只返回集合的小“分片”。