Docs Menu

$pop

이 페이지의 내용

$pop

The $pop operator removes the first or last element of an array. Pass $pop a value of -1 to remove the first element of an array and 1 to remove the last element in an array.

$pop 연산자의 형식은 다음과 같습니다.

{ $pop: { <field>: <-1 | 1>, ... } }

내장된 문서나 배열에 <field> 기호를 지정하려면 점 표기법을사용하십시오.

MongoDB 5.0부터 업데이트 연산자는 문자열 기반 이름이 있는 문서 필드를 사전순으로 처리합니다. 숫자 이름이 있는 필드는 숫자 순서대로 처리됩니다. 자세한 내용은 업데이트 운영자 동작을 참조하십시오.

The $pop operation fails if the <field> is not an array.

If the $pop operator removes the last item in the <field>, the <field> will then hold an empty array.

MongoDB 5.0부터 빈 피연산자 표현식( { } )과 함께 $pop과 같은 업데이트 연산자를 사용할 때 mongod에서 더 이상 오류가 발생하지 않습니다. 업데이트가 비어 있으면 변경 사항이 없으며 oplog 항목이 생성되지 않습니다(즉, 작업이 작동하지 않음을 의미합니다).

students 컬렉션을 생성합니다.

db.students.insertOne( { _id: 1, scores: [ 8, 9, 10 ] } )

The following example removes the first element, 8, from the scores array:

db.students.updateOne( { _id: 1 }, { $pop: { scores: -1 } } )

The first element, 8, has been removed from the scores array:

{ _id: 1, scores: [ 9, 10 ] }

다음 문서를 students 컬렉션에 추가합니다:

db.students.insertOne( { _id: 10, scores: [ 9, 10 ] } )

The following example removes the last element, 10, from the scores array by specifying 1 in the $pop expression:

db.students.updateOne( { _id: 10 }, { $pop: { scores: 1 } } )

The last element, 10, has been removed from the scores array:

{ _id: 10, scores: [ 9 ] }

이 페이지의 내용