On this page本页内容
$slice
¶The $slice
modifier limits the number of array elements during a $push
operation. $slice
修饰符在$push
操作期间限制数组元素的数量。To project, or return, a specified number of array elements from a read operation, see the 要从读取操作中投影或返回指定数量的数组元素,请参阅$slice
projection operator instead.$slice
投影运算符。
To use the 要使用$slice
modifier, it must appear with the $each
modifier. $slice
修饰符,它必须与$each
修饰符一起出现。You can pass an empty array 可以将空数组[]
to the $each
modifier such that only the $slice
modifier has an effect.[]
传递给$each
修饰符,以便只有$slice
修饰符具有效果。
The <num>
can be:<num>
可以是:
<field> to an empty array.<field> 更新为空数组。 | |
<field> to contain only the last <num> elements.<field> 以仅包含最后的<num> 元素。 | |
<field> contain only the first <num> elements.<field> 以仅包含前<num> 元素。 |
The order in which the modifiers appear is immaterial. 修饰语出现的顺序无关紧要。Previous versions required the 以前的版本要求$each
modifier to appear as the first modifier if used in conjunction with $slice
. $each
修饰符在与$slice
一起使用时显示为第一个修饰符。For a list of modifiers available for 有关$push
, see Modifiers.$push
可用的修饰符列表,请参阅修饰符。
Trying to use the 尝试在没有$slice
modifier without the $each
modifier results in an error.$each
修饰符的情况下使用$slice
修饰符会导致错误。
A collection 集合students
contains the following document:students
包括以下文档:
The following operation adds new elements to the 以下操作向scores
array, and then uses the $slice
modifier to trim the array to the last five elements:scores
数组添加新元素,然后使用$slice
修饰符将数组修剪为最后五个元素:
The result of the operation is slice the elements of the updated 操作的结果是将更新的scores
array to the last five elements:scores
数组的元素切片为最后五个元素:
A collection 集合students
contains the following document:students
包括以下文件:
The following operation adds new elements to the 下面的操作向scores
array, and then uses the $slice
modifier to trim the array to the first three elements.scores
数组添加新元素,然后使用$slice
修饰符将数组修剪为前三个元素。
The result of the operation is to slice the elements of the updated 操作的结果是将更新后的scores
array to the first three elements:scores
数组的元素切片为前三个元素:
A collection 学生students
contains the following document:students
包括以下文档:
To update the 要仅使用scores
field with just the effects of the $slice
modifier, specify the number of elements to slice (e.g. -3
) for the $slice
modifier and an empty array []
for the $each
modifier, as in the following:$slice
修饰符的效果更新scores
字段,请为$slice
修饰符指定要切片的元素数(例如-3
),为$each
修饰符指定空数组[]
,如下所示:
The result of the operation is to slice the elements of the 操作的结果是将scores
array to the last three elements:scores
数组的元素切片为最后三个元素:
$slice
with Other $push
Modifiers$slice
与其他$push
修饰符一起使用¶A collection 集合students
has the following document:students
有以下文档:
The following 以下$push
operation uses:$push
操作使用:
$each
modifier to add multiple documents to the quizzes
array,$each
修饰符将多个文档添加到quizzes
数组中,$sort
modifier to sort all the elements of the modified quizzes
array by the score
field in descending order, and$sort
修饰符,用于按score
字段降序排列修改后的quizzes
数组的所有元素,以及$slice
modifier to keep only the first three sorted elements of the quizzes
array.$slice
修饰符只保留测验数组的前三个排序元素。The result of the operation is keep only the three highest scoring quizzes:该操作的结果是只保留三个得分最高的测验:
The order of the modifiers is immaterial to the order in which the modifiers are processed. 修饰语的顺序与修饰语的处理顺序无关。See Modifiers for details.有关详细信息,请参阅修饰符。