On this page本页内容
The $slice
has one of the following syntax forms:$slice
具有以下语法形式之一:
or或者
Value | |
---|---|
$slice: <number> |
|
$slice: [ <number to skip>, <number to return> ] |
|
$slice
of Embedded Array$slice
¶Starting in MongoDB 4.4, the 从MongoDB 4.4开始,嵌套文档中数组的$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.$slice
投影不再返回嵌套文档中的其他字段,因为该投影是包含投影的一部分。
For example, consider a collection 例如,考虑包含inventory
with documents that contain a size
field:size
字段的文档的集合inventory
:
Starting in MongoDB 4.4, the following operation projects the 从MongoDB 4.4开始,以下操作仅使用_id
field (by default), the qty
field, and the details
field with just the specified slice of the colors
array:colors
数组的指定部分投影_id
字段(默认情况下)、qty
字段和details
字段:
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. $slice
投影是排除投影的一部分,则该操作将继续返回嵌套文档中的其他字段。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._id
字段和colors
数组中位于指定切片之外的元素,并返回所有其他字段。
The $slice
projection by itself is considered an exclusion.$slice
投影本身被认为是一种排除。
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.$slice
投影还包括嵌套文档中的其他字段,无论该投影是包含还是排除。
视图上的db.collection.find()
operations on views do not support $slice
projection operator.db.collection.find()
操作不支持$slice
投影运算符。
$
Positional Operator and $slice
Restriction$
位置运算符和$slice
限制¶Starting in MongoDB 4.4, 从MongoDB 4.4开始,find
and findAndModify
projection cannot include $slice
projection expression as part of a $
projection expression.find
和findAndModify
投影不能将$slice
投影表达式作为$
投影表达式的一部分。
For example, starting in MongoDB 4.4, the following operation is invalid:例如,从MongoDB 4.4开始,以下操作无效:
MongoDB already has a MongoDB已经有一个限制,顶级字段名不能以美元符号(restriction
where top-level field names cannot start with the dollar sign ($
).$
)开头。
In previous versions, MongoDB returns the first element (在以前的版本中,MongoDB返回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. instock
数组中与查询条件匹配的第一个元素(instock.$
);亦即,位置投影"instock.$"
优先,而$slice:1
是不可操作的。The "instock.$": { $slice: 1 }
does not exclude any other document field."instock.$": { $slice: 1 }
不排除任何其他文档字段。
$slice
of an Array and Embedded Fields$slice
和嵌入字段¶Starting in MongoDB 4.4, 从MongoDB 4.4开始,find
and findAndModify
projection cannot contain both a $slice
of an array and a field embedded in the array.find
和findAndModify
投影不能同时包含数组的$slice
和数组中嵌入的字段。
For example, consider a collection 例如,考虑包含数组字段inventory
that contains an array field instock
:instock
的集合inventory
:
Starting in MongoDB 4.4, the following operation fails with a 从MongoDB 4.4开始,以下操作失败,出现“路径冲突”错误: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. instock
数组中的第一个元素($slice:1
),但不显示投影元素中的warehouse
字段。Starting in MongoDB 4.4, to achieve the same result, use the 从MongoDB 4.4开始,为了获得相同的结果,使用db.collection.aggregate()
method with two separate $project
stages.db.collection.aggregate()
方法,包含两个单独的$project
阶段。
See also参阅
Create an example collection 使用以下文档创建一个示例集合posts
with the following documents:post
:
The following operation uses the 下面的操作使用$slice
projection operator on the comments
array to return the array with its first three elements. comments
数组上的$slice
投影运算符返回包含前三个元素的数组。If the array has less than three elements, all elements in the array are returned.如果数组少于三个元素,则返回数组中的所有元素。
The operation returns the following documents:该操作将返回以下文档:
The following operation uses the 下面的操作使用$slice
projection operator on the comments
array to return the array with its last three elements. comments
数组上的$slice
投影运算符返回带有最后三个元素的数组。If the array has less than three elements, all elements in the array are returned.如果数组少于三个元素,则返回数组中的所有元素。
The operation returns the following documents:该操作将返回以下文档:
The following operation uses the 以下操作使用$slice
projection operator on the comments
array to:comments
数组上的$slice
投影运算符:
If the array has less than three elements after the skip, all remaining elements are returned.如果跳过后数组中的元素少于三个,则返回所有剩余的元素。
The operation returns the following documents:该操作将返回以下文档:
The following operation uses the 下面的操作使用$slice
projection operator on the comments
array tocomments
数组上的$slice
投影运算符
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:该操作将返回以下文档: