Database Manual / Reference / Query Language / Aggregation Stages

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

Definition定义

$skip

Skips over the specified number of documents that pass into the stage and passes the remaining documents to the next stage in the pipeline.跳过传递到该阶段的指定数量的文档,并将剩余文档传递到管道中的下一阶段。

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

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

$skip takes a positive integer that specifies the maximum number of documents to skip.取一个正整数,指定要跳过的最大文档数。

Note

Starting in MongoDB 5.0, the $skip 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开始,$skip管道聚合具有64位整数限制。传递给管道的值超过此限制将返回无效的参数错误。

Behavior行为

Using $skip with Sorted Results在排序结果中使用$skip

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

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

Sorting on fields that contain duplicate values may return a different 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([
{ $skip : 5 }
]);

This operation skips the first 5 documents passed to it by the pipeline. $skip has no effect on the content of the documents it passes along the pipeline.此操作跳过管道传递给它的前5个文档。$skip对它在管道中传递的文档内容没有影响。

C#

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

The following example creates a pipeline stage that skips the first five documents in the input and passes the remaining documents to the next stage in the pipeline:以下示例创建了一个管道阶段,该阶段跳过输入中的前五个文档,并将剩余文档传递给管道中的下一个阶段:

 var pipeline = new EmptyPipelineDefinition<Movie>()
.Skip(5);
Node.js

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

The following example creates a pipeline stage that skips the first five documents in the input collection and passes the remaining documents to the next stage in the pipeline. The example then runs the aggregation pipeline:以下示例创建了一个管道阶段,该阶段跳过输入集合中的前五个文档,并将剩余文档传递给管道中的下一个阶段。然后,该示例运行聚合管道:

const pipeline = [{ $skip: 5 }];

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

Learn More了解更多

To see full aggregation examples that use the $skip stage, see the Complete Aggregation Pipeline Tutorials.要查看使用$skip阶段的完整聚合示例,请参阅完整的聚合管道教程