- Reference >
- Operators >
- Query and Projection Operators >
- Projection Operators >
- $slice (projection)
$slice (projection)¶
On this page
Definition¶
Syntax¶
The $slice
has one of the following syntax forms:
or
Value | Description |
---|---|
$slice: <number> |
Specifies the number of elements to return in the
If the |
$slice: [ <number to skip>, <number to return> ] |
Specifies the number of elements to return in the
For the
For the |
Behavior¶
$slice
of Embedded Array¶
Starting in MongoDB 4.4, the $slice
projection of an
array in an nested document no longer returns the other fields in
the nested document when the projection is part of an inclusion
projection.
For example, consider a collection inventory
with documents that
contain a size
field:
Starting in MongoDB 4.4, the following operation projects the
_id
field (by default), the qty
field, and the details
field with just the specified slice of the colors
array:
That is, the operation returns the following document:
If the $slice
projection is part of an exclusion
projection, the operation continues to return the other fields in
the nested document. That is, the following projection is an
exclusion projection. The projection excludes the _id
field and
the elements in the colors
array that fall outside the specified
slice and returns all other fields.
The $slice
projection by itself is considered an exclusion.
In previous versions, the $slice
projection also
include the other fields in the nested document regardless of
whether the projection is an inclusion or an exclusion.
View Restriction¶
db.collection.find()
operations on views do not support $slice
projection operator.
$
Positional Operator and $slice
Restriction¶
Starting in MongoDB 4.4, find
and findAndModify
projection
cannot include $slice
projection expression as part of a
$
projection expression.
For example, starting in MongoDB 4.4, the following operation is invalid:
MongoDB already has a restriction
where top-level field names cannot start with the dollar sign
($
).
In previous versions, MongoDB returns the first element
(instock.$
) in the instock
array that matches the query
condition; i.e. the positional projection "instock.$"
takes
precedence and the $slice:1
is a no-op. The "instock.$": {
$slice: 1 }
does not exclude any other document field.
Path Collision: $slice
of an Array and Embedded Fields¶
Starting in MongoDB 4.4, find
and findAndModify
projection
cannot contain both a $slice
of an array and a field
embedded in the array.
For example, consider a collection inventory
that contains an array
field instock
:
Starting in MongoDB 4.4, the following operation fails with a Path
collision
error:
In previous versions, the projection applies both projections and
returns the first element ($slice: 1
) in the instock
array
but suppresses the warehouse
field in the projected element.
Starting in MongoDB 4.4, to achieve the same result, use the
db.collection.aggregate()
method with two separate
$project
stages.
See also
Examples¶
Create an example collection posts
with the following documents:
Return an Array with Its First 3 Elements¶
The following operation uses the $slice
projection
operator on the comments
array to return the array with its first
three elements. If the array has less than three elements, all elements
in the array are returned.
The operation returns the following documents:
Return an Array with Its Last 3 Elements¶
The following operation uses the $slice
projection
operator on the comments
array to return the array with its last
three elements. If the array has less than three elements, all elements
in the array are returned.
The operation returns the following documents:
Return an Array with 3 Elements After Skipping the First Element¶
The following operation uses the $slice
projection
operator on the comments
array to:
- Skip the first element such that the second element is the starting point.
- Then, return three elements from the starting point.
If the array has less than three elements after the skip, all remaining elements are returned.
The operation returns the following documents:
Return an Array with 3 Elements After Skipping the Last Element¶
The following operation uses the $slice
projection
operator on the comments
array to
- Skip backwards from the first element such that the last element is the starting point.
- Then, return three elements from the starting point.
If the array has less than three elements after the skip, all remaining elements in the array are returned.
The operation returns the following documents: