- Reference >
- Operators >
- Update Operators >
- Array Update Operators >
- $slice
$slice¶
-
$slice¶ New in version 2.4.
The
$slicemodifier limits the number of array elements during a$pushoperation. To project, or return, a specified number of array elements from a read operation, see the$sliceprojection operator instead.To use the
$slicemodifier, it must appear with the$eachmodifier, and the$eachmodifier must be the first modifier for the$pushoperation.The
<num>is either a negative number or zero:- If
<num>is negative, the array<field>contains only the last<num>elements. - If
<num>is zero, the array<field>is an empty array.
- If