Database Manual / Reference / Query Language / Aggregation Stages

$limit (aggregation stage)(聚合阶段)

Definition定义

$limit
Limits the number of documents passed to the next stage in the pipeline.

Compatibility兼容性

You can use $limit for deployments hosted in the following environments:您可以将$limit用于在以下环境中托管的部署:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Syntax语法

The $limit stage has the following prototype form:$limit阶段具有以下原型形式:

{ $limit: <positive 64-bit integer> }

$limit takes a positive integer that specifies the maximum number of documents to pass along.取一个正整数,指定要传递的最大文档数。

Note

Starting in MongoDB 5.0, the $limit pipeline aggregation has a 64-bit integer limit. Values passed to the pipeline which exceed this limit will return a invalid argument error.从MongoDB 5.0开始,$limit管道聚合具有64位整数限制。传递给管道的值超过此限制将返回无效的参数错误。

Behavior行为

Using $limit with Sorted Results对排序结果使用$limit

If using the $limit stage with any of:如果将$limit阶段与以下任何一项一起使用:

be sure to include at least one field in your sort that contains unique values, before passing results to the $limit stage.在将结果传递给$limit阶段之前,请确保在排序中至少包含一个包含唯一值的字段。

Sorting on fields that contain duplicate values may return an inconsistent sort order for those duplicate fields over multiple executions, especially when the collection is actively receiving writes.对包含重复值的字段进行排序可能会在多次执行中为这些重复字段返回不一致的排序顺序,特别是在集合正在积极接收写入时。

The easiest way to guarantee sort consistency is to include the _id field in your sort query.保证排序一致性的最简单方法是在排序查询中包含_id字段。

See the following for more information on each:有关每个的更多信息,请参阅以下内容:

Examples示例

MongoDB Shell

Consider the following example:考虑以下示例:

db.article.aggregate([
{ $limit : 5 }
]);

This operation returns only the first 5 documents passed to it by the pipeline. $limit has no effect on the content of the documents it passes.此操作仅返回管道传递给它的前5个文档。$limit对它传递的文档内容没有影响。

C#

To use the MongoDB .NET/C# driver to add a $limit stage to an aggregation pipeline, call the Limit() method on a PipelineDefinition object.要使用MongoDB NET/C#驱动程序向聚合管道添加$limit阶段,请在PipelineDefinition对象上调用Limit()方法。

The following example creates a pipeline stage that limits the number of returned documents to 10:以下示例创建了一个管道阶段,将返回的文档数量限制为10个:

var pipeline = new EmptyPipelineDefinition<BsonDocument>()
.Limit(10);
Node.js

To use the MongoDB Node.js driver to add a $limit stage to an aggregation pipeline, use the $limit operator in a pipeline object.要使用MongoDB Node.js驱动程序向聚合管道添加$limit阶段,请在管道对象中使用$limit运算符。

The following example creates a pipeline stage that limits the number of returned documents to 10. The example then runs the aggregation pipeline:以下示例创建了一个管道阶段,将返回的文档数量限制为10个。然后,该示例运行聚合管道:

const pipeline = [{ $limit: 10 }];

const cursor = collection.aggregate(pipeline);
return cursor;

Note

When a $sort precedes a $limit and there are no intervening stages that modify the number of documents, the optimizer can coalesce the $limit into the $sort. $sort位于$limit之前,并且没有修改文档数量的中间阶段时,优化器可以将$limit合并到$sort中。This allows the $sort operation to only maintain the top n results as it progresses, where n is the specified limit, and ensures that MongoDB only needs to store n items in memory. 这允许$sort操作在执行过程中只维护前n个结果,其中n是指定的限制,并确保MongoDB只需要在内存中存储n个项目。This optimization still applies when allowDiskUse is true and the n items exceed the aggregation memory limit.allowDiskUsetrue并且n个项目超过聚合内存限制时,此优化仍然适用。

Learn More了解更多

To learn how to use $limit in a full example, see the Filter Data tutorial.要了解如何在完整示例中使用$limit,请参阅筛选数据教程。