$skip

The `$skip` stage in the aggregation pipeline is used to skip a specified number of documents from the input and pass the remaining documents to the next stage in the pipeline. This stage is useful for implementing pagination in queries.

Syntax

{
  $skip: <number>
}

Parameters

numbernumberrequired

The number of documents to skip before passing the remaining documents to the next stage.

Examples

Sample Data

{
  "_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
  "name": "First Up Consultants | Beverage Shop",
  "sales": { "totalSales": 75670 }
}

Skipping documents in a collection

Skip the first 2 documents and return the rest.

This query skips 2 documents and returns all remaining documents.

Query:

db.stores.aggregate([
  { $skip: 2 }
])

Output:

[
  {
    "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
    "store": { "name": "Downtown Store" }
  }
]

Skipping documents and then limiting the result

Skip the first 2 documents and then limit the result to the next 3 documents.

This query combines $skip with $limit for pagination.

Query:

db.stores.aggregate([
  { $skip: 2 },
  { $limit: 3 }
])

Output:

[
  {
    "_id": "728c068a-638c-40af-9172-8ccfa7dddb49",
    "name": "Contoso, Ltd. | Book Store",
    "sales": { "revenue": 34879 }
  }
]

Related