$sliceThe
$slicemodifier limits the number of array elements during a$pushoperation. To project, or return, a specified number of array elements from a read operation, see the$sliceprojection operator instead.To use the
$slicemodifier, it must appear with the$eachmodifier. You can pass an empty array[]to the$eachmodifier such that only the$slicemodifier has an effect.{ $push: { <field>: { $each: [ <value1>, <value2>, ... ], $slice: <num> } } } <num>は次のとおりです。値説明Zero
配列
<field>を空の配列に更新します。負
配列
<field>を更新して、最後の<num>要素のみを含めるようにします。正の
配列
<field>には最初の<num>要素のみが含まれる配列を更新するには、
動作
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.
例
配列の末尾からのスライス
コレクション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 } } } )
操作の結果は、更新されたscores配列の要素を最後の 5 つの要素にスライスします。
{ "_id" : 1, "scores" : [ 50, 60, 80, 78, 86 ] }
配列の先頭からのスライス
コレクション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 } } } )
操作の結果は、更新されたscores配列の要素を最初の 3 つの要素にスライスします。
{ "_id" : 2, "scores" : [ 89, 90, 100 ] }
スライスのみを使用して配列を更新
コレクション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 } } } )
操作の結果は、 scores配列の要素を最後の 3 つの要素にスライスすることです。
{ "_id" : 3, "scores" : [ 70, 100, 20 ] }
他の $push 修飾子とともに $slice を使用する
次のドキュメントを 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修飾子、変更された
quizzes配列のすべての要素をscoreフィールドで降順にソートするための$sort修飾子、the
$slicemodifier to keep only the first three sorted elements of thequizzesarray.
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 } } } )
操作後、配列には最高得点の 3 つのクイズだけが残ります。
{ "_id" : 5, "quizzes" : [ { "wk" : 1, "score" : 10 }, { "wk" : 2, "score" : 8 }, { "wk" : 5, "score" : 8 } ] }
修飾子の順序は、修飾子が処理される順序には影響しません。 詳細については、「修飾子」を参照してください。