OPTIONS
翻译或纠错本页面

$limit (aggregation)

Definition

$limit

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

The $limit stage has the following prototype form:

{ $limit: <positive integer> }

$limit takes a positive integer that specifies the maximum number of documents to pass along.

Example

Consider the following example:

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

This operation returns only the first 5 documents passed to it from by the pipeline. $limit has no effect on the content of the documents it passes.

注解

When a $sort immediately precedes a $limit in the pipeline, the $sort operation only maintains the top n results as it progresses, where n is the specified limit, and MongoDB only needs to store n items in memory. This optimization still applies when allowDiskUse is true and the n items exceed the aggregation memory limit.

在 2.4 版更改: Before MongoDB 2.4, $sort would sort all the results in memory, and then limit the results to n results.