> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

# $slice  (expression operator)

## Definition

Returns a subset of an array.

**Note: Disambiguation**

For information on limiting the size of an array during an update with [`$push`](https://www.mongodb.com/docs/manual/reference/operator/update/push.md#mongodb-update-up.-push), see the [`$slice`](https://www.mongodb.com/docs/manual/reference/operator/update/slice.md#mongodb-update-up.-slice) update modifier instead.

For the find command projection operator, see the [`$slice`](https://www.mongodb.com/docs/manual/reference/operator/projection/slice.md#mongodb-projection-proj.-slice) projection operator instead.

[`$slice`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/slice.md#mongodb-expression-exp.-slice) has one of two syntax forms:

The following syntax returns elements from either the start or end of the array:

```javascript
{ $slice: [ <array expression>, <n> ] }
```

The following syntax returns elements from the specified position in the array:

```javascript
{ $slice: [ <array expression>, <position>, <n> ] }
```

| Operand | Description |
| --- | --- |
| `<array expression>` | Any valid [expression](https://www.mongodb.com/docs/manual/reference/mql/expressions.md#std-label-aggregation-expressions) if it resolves to an array. |
| `<position>` | Optional. Any valid [expression](https://www.mongodb.com/docs/manual/reference/mql/expressions.md#std-label-aggregation-expressions) if it resolves to an integer. If positive, [`$slice`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/slice.md#mongodb-expression-exp.-slice) determines the starting position from the start of the array. If `<position>` is greater than the number of elements, the [`$slice`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/slice.md#mongodb-expression-exp.-slice) returns an empty array.; If negative, [`$slice`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/slice.md#mongodb-expression-exp.-slice) determines the starting position from the end of the array. If the absolute value of the `<position>` is greater than the number of elements, the starting position is the start of the array. |
| `<n>` | Any valid [expression](https://www.mongodb.com/docs/manual/reference/mql/expressions.md#std-label-aggregation-expressions) as long as it resolves to an integer. If `<position>` is specified, `<n>` must resolve to a positive integer. If positive, [`$slice`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/slice.md#mongodb-expression-exp.-slice) returns up to the first `n` elements in the array. If the `<position>` is specified, [`$slice`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/slice.md#mongodb-expression-exp.-slice) returns the first `n` elements starting from the position.; If negative, [`$slice`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/slice.md#mongodb-expression-exp.-slice) returns up to the last `n` elements in the array. `n` cannot resolve to a negative number *if* `<position>` is specified. |

For more information on expressions, see [Expressions.](https://www.mongodb.com/docs/manual/reference/mql/expressions.md#std-label-aggregation-expressions)

## Behavior

| Example | Results |
| --- | --- |
| `{ $slice: [ [ 1, 2, 3 ], 1, 1 ] }` | `[ 2 ]` |
| `{ $slice: [ [ 1, 2, 3 ], -2 ] }` | `[ 2, 3 ]` |
| `{ $slice: [ [ 1, 2, 3 ], 15, 2 ] }` | `[  ]` |
| `{ $slice: [ [ 1, 2, 3 ], -15, 2 ] }` | `[ 1, 2 ]` |

## Example

A collection named `users` contains the following documents:

```javascript
db.users.insertMany( [
   { _id: 1, name: "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] },
   { _id: 2, name: "li", favorites: [ "apples", "pudding", "pie" ] },
   { _id: 3, name: "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] },
   { _id: 4, name: "ty", favorites: [ "ice cream" ] }
] )
```

The following example returns at most the first three elements in the `favorites` array for each user:

```javascript
db.users.aggregate([
   { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } }
])
```

The operation returns the following results:

```javascript
[
   { _id: 1, name: "dave123", threeFavorites: [ "chocolate", "cake", "butter" ] },
   { _id: 2, name: "li", threeFavorites: [ "apples", "pudding", "pie" ] },
   { _id: 3, name: "ahn", threeFavorites: [ "pears", "pecans", "chocolate" ] },
   { _id: 4, name: "ty", threeFavorites: [ "ice cream" ] }
]
```
