$pop
정의
행동
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 항목이 생성되지 않습니다(즉, 작업이 작동하지 않음을 의미합니다).
예시
Remove the First Item of an Array
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 ] }
Remove the Last Item of an Array
다음 문서를 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 ] }