AI 에이전트의 경우: 문서 인덱스는 https://www.mongodb.com/ko-kr/docs/llms.txt에서 사용할 수 있으며, 모든 페이지의 마크다운 버전은 어떤 URL 경로에 .md를 추가하여 사용할 수 있습니다.
Docs Menu

$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>
}
}
}

<num>은 다음과 같을 수 있습니다.

설명

Zero

배열 <field> 를 빈 배열로 업데이트합니다.

음수

마지막 <num> 요소만 포함하도록 <field> 배열을 업데이트합니다.

양수

배열 <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 ] }

다음 문서를 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 $slice modifier to keep only the first three sorted elements of the quizzes 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 }
]
}

수정자의 순서는 수정자가 처리되는 순서에 중요하지 않습니다. 자세한 내용은 수정자를 참조하세요.

이 페이지 평가하기

이 페이지의 내용