$slice
$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.To use the
$slice
modifier, it must appear with the$each
modifier. You can pass an empty array[]
to the$each
modifier such that only the$slice
modifier has an effect.{ $push: { <field>: { $each: [ <value1>, <value2>, ... ], $slice: <num> } } } The
<num>
can be:값설명Zero
To update the array
<field>
to an empty array.Negative
To update the array
<field>
to contain only the last<num>
elements.Positive
To update the array
<field>
contain only the first<num>
elements.
행동
MongoDB 5.0부터 업데이트 연산자는 문자열 기반 이름이 있는 문서 필드를 사전순으로 처리합니다. 숫자 이름이 있는 필드는 숫자 순서대로 처리됩니다. 자세한 내용은 업데이트 운영자 동작을 참조하십시오.
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.
Trying to use the $slice
modifier without the $each
modifier results in an error.
예시
Slice from the End of the Array
컬렉션 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:
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:
{ "_id" : 1, "scores" : [ 50, 60, 80, 78, 86 ] }
Slice from the Front of the Array
컬렉션 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.
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:
{ "_id" : 2, "scores" : [ 89, 90, 100 ] }
Update Array Using Slice Only
컬렉션 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:
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:
{ "_id" : 3, "scores" : [ 70, 100, 20 ] }
$slice
을(를) 다른 $push
수정자와 함께 사용
다음 문서를 students
컬렉션에 추가합니다:
db.students.insertOne( { "_id" : 5, "quizzes" : [ { "wk": 1, "score" : 10 }, { "wk": 2, "score" : 8 }, { "wk": 3, "score" : 5 }, { "wk": 4, "score" : 6 } ] } )
다음 $push
작업에서는 다음을 사용합니다.
quizzes
배열에 여러 문서를 추가하는
$each
수정자,
the
$sort
modifier to sort all the elements of the modifiedquizzes
array by thescore
field in descending order, andthe
$slice
modifier to keep only the first three sorted elements of thequizzes
array.
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 } } } )
작업 후에는 가장 높은 점수를 받은 세 개의 퀴즈만 배열에 표시됩니다:
{ "_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.