$slice
On this page本页内容
$slice
-
The$slice
modifier limits the number of array elements during a$push
operation. To project, or return, a specified number of array elements from a read operation, see the$slice
projection operator instead.$slice
修饰符在$push
操作期间限制数组元素的数量。要从读取操作投影或返回指定数量的数组元素,请参阅$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
修饰符才有效果。{
$push: {
<field>: {
$each: [ <value1>, <value2>, ... ],
$slice: <num>
}
}
}The<num>
can be:<num>
可以是:Value值Description描述Zero零To update the array将数组<field>
to an empty array.<field>
更新为空数组。Negative负数To update the array将数组<field>
to contain only the last<num>
elements.<field>
更新为只包含最后的<num>
元素。Positive正数To update the array要更新数组,<field>
contain only the first<num>
elements.<field>
只包含前<num>
个元素。
Behavior行为
Starting in MongoDB 5.0, update operators process document fields with string-based names in lexicographic order. 从MongoDB 5.0开始,update运算符按照字典顺序处理具有基于字符串的名称的文档字段。Fields with numeric names are processed in numeric order. 具有数字名称的字段按数字顺序处理。See Update Operators Behavior for details.有关详细信息,请参阅更新运算符行为。
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
. For a list of modifiers available for $push
, see Modifiers.$each
修饰符在与$slice
一起使用时显示为第一个修饰符。有关$push
可用的修改器列表,请参阅修饰符。
Trying to use the 尝试在没有$slice
modifier without the $each
modifier results in an error.$slice
修饰符的情况下使用$each
修饰符会导致错误。
Examples实例
Slice from the End of the Array从数组末端分片
A collection students
contains the following document:students
集合包含以下文档:
{ "_id" : 1, "scores" : [ 40, 50, 60 ] }
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
修饰符将数组修剪为最后五个元素:
db.students.updateOne(
{ _id: 1 },
{
$push: {
scores: {
$each: [ 80, 78, 86 ],
$slice: -5
}
}
}
)
The result of the operation is slice the elements of the updated 操作的结果是将更新的scores
array to the last five elements:scores
数组的元素分片为最后五个元素:
{ "_id" : 1, "scores" : [ 50, 60, 80, 78, 86 ] }
Slice from the Front of the Array数组前面的分片
A collection students
contains the following document:students
集合包含以下文档:
{ "_id" : 2, "scores" : [ 89, 90 ] }
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.$slice
修饰符将数组修剪为前三个元素。
db.students.updateOne(
{ _id: 2 },
{
$push: {
scores: {
$each: [ 100, 20 ],
$slice: 3
}
}
}
)
The result of the operation is to slice the elements of the updated 操作的结果是将更新的scores
array to the first three elements:scores
数组的元素分片为前三个元素:
{ "_id" : 2, "scores" : [ 89, 90, 100 ] }
Update Array Using Slice Only仅使用分片更新数组
A collection students
contains the following document:students
集合包含以下文档:
{ "_id" : 3, "scores" : [ 89, 70, 100, 20 ] }
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
修改器指定空数组[]
,如下所示:
db.students.updateOne(
{ _id: 3 },
{
$push: {
scores: {
$each: [ ],
$slice: -3
}
}
}
)
The result of the operation is to slice the elements of the 操作的结果是将scores
array to the last three elements:scores
数组的元素分片为最后三个元素:
{ "_id" : 3, "scores" : [ 70, 100, 20 ] }
Use $slice
with Other $push
Modifiers将$slice
与其他$push
修饰符一起使用
$slice
with Other $push
ModifiersAdd the following document to the 将以下文档添加到students
collection:students
集合中:
db.students.insertOne(
{
"_id" : 5,
"quizzes" : [
{ "wk": 1, "score" : 10 },
{ "wk": 2, "score" : 8 },
{ "wk": 3, "score" : 5 },
{ "wk": 4, "score" : 6 }
]
}
)
The following 以下$push
operation uses:$push
操作使用:
the$each
modifier to add multiple documents to thequizzes
array,$each
修饰符将多个文档添加到quizzes
数组,the$sort
modifier to sort all the elements of the modifiedquizzes
array by thescore
field in descending order, and$sort
修饰符,用于按score
字段降序对修改后的quizzes
数组的所有元素进行排序,以及the$slice
modifier to keep only the first three sorted elements of thequizzes
array.$slice
修饰符只保留quizzes
数组的前三个排序元素。
db.students.updateOne(
{ _id: 5 },
{
$push: {
quizzes: {
$each: [ { wk: 5, score: 8 }, { wk: 6, score: 7 }, { wk: 7, score: 6 } ],
$sort: { score: -1 },
$slice: 3
}
}
}
)
After the operation only the three highest scoring quizzes are in the array:手术后,数组中只有三个得分最高的测验:
{
"_id" : 5,
"quizzes" : [
{ "wk" : 1, "score" : 10 },
{ "wk" : 2, "score" : 8 },
{ "wk" : 5, "score" : 8 }
]
}
The order of the modifiers is immaterial to the order in which the modifiers are processed. 修饰语的顺序与修饰语的处理顺序无关。See Modifiers for details.有关详细信息,请参阅修饰符。