$slice

The $slice operator is used to return a subset of an array. It can be used to limit the number of elements in an array to a specified number or to return elements from a specified position in the array. The operator is useful when dealing with large arrays where only a portion of the data is needed for processing or display.

Syntax

The syntax for the $slice operator is as following.

  • Returns elements from either the start or end of the array
{ $slice: [ <array>, <n> ] }
  • Returns elements from the specified position in the array
{ $slice: [ <array>, <position>, <n> ] }

Parameters

Parameter Description
array Any valid array expression.
position Any valid integer expression.
n Any valid integer expression.

Example

Let's understand the usage with sample json from stores dataset.

{
  "_id": "988d2dd1-2faa-4072-b420-b91b95cbfd60",
  "name": "Lakeshore Retail",
  "sales": {
    "salesByCategory": [
      {
        "categoryName": "Towel Racks",
        "totalSales": 13237
      },
      {
        "categoryName": "Washcloths",
        "totalSales": 44315
      },
      {
        "categoryName": "Face Towels",
        "totalSales": 42095
      },
      {
        "categoryName": "Toothbrush Holders",
        "totalSales": 47912
      },
      {
        "categoryName": "Hybrid Mattresses",
        "totalSales": 48660
      },
      {
        "categoryName": "Napkins",
        "totalSales": 31439
      },
      {
        "categoryName": "Pillow Cases",
        "totalSales": 38833
      }
    ]},
"tag": [
    '#ShopLocal',
    '#FashionStore',
    '#SeasonalSale',
    '#FreeShipping',
    '#MembershipDeals'
  ]
}

Example 1: Return the set of elements from the array

The example queries for the first three elements of the sales.salesByCategory array for _id: 988d2dd1-2faa-4072-b420-b91b95cbfd60 within stores collection.

db.stores.aggregate([
     { $match: { "_id": "988d2dd1-2faa-4072-b420-b91b95cbfd60"} }
   , { $project: { "salesByCategory": { $slice: [ "$sales.salesByCategory", 3 ] } } }
])

The query response reverts with first three array elements for the sample json.

{
  "_id": "988d2dd1-2faa-4072-b420-b91b95cbfd60",
  "salesByCategory": [
    {
      "categoryName": "Towel Racks",
      "totalSales": 13237
    },
    {
      "categoryName": "Washcloths",
      "totalSales": 44315
    },
    {
      "categoryName": "Face Towels",
      "totalSales": 42095
    }
  ]
}