문서 메뉴
문서 홈
/
MongoDB 매뉴얼
/ / / /

$pull

이 페이지의 내용

  • 호환성
  • 구문
  • 행동
  • 예제
$pull

$pull 연산자는 지정된 조건과 일치하는 값 또는 특정 값의 모든 인스턴스를 기존 배열에서 제거합니다.

다음 환경에서 호스팅되는 배포에 $pull 사용할 수 있습니다.

  • MongoDB Atlas: 클라우드에서의 MongoDB 배포를 위한 완전 관리형 서비스

  • MongoDB Enterprise: MongoDB의 구독 기반 자체 관리 버전

  • MongoDB Community: MongoDB의 소스 사용 가능 무료 자체 관리 버전

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

{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }

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

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

<condition>을 지정하고 배열 요소가 내장된 문서인 경우 $pull 연산자는 각 배열 요소가 컬렉션의 문서인 것처럼 <condition>을 적용합니다. 예제는 Remove All items that match a specified $pull condition with bulkWrite() 를 참조하십시오.

제거 대상으로 지정된 <value>이 배열인 경우 $pull은 순서를 포함하여 지정된 <value>과 정확히 일치하는 배열 요소만 제거합니다.

제거할 대상으로 지정된 <value>가 문서인 경우 $pull은 배열에서 정확히 동일한 필드와 값을 가진 요소만 제거합니다. 필드의 순서는 다를 수 있습니다.

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

stores 컬렉션을 생성합니다.

db.stores.insertMany( [
{
_id: 1,
fruits: [ "apples", "pears", "oranges", "grapes", "bananas" ],
vegetables: [ "carrots", "celery", "squash", "carrots" ]
},
{
_id: 2,
fruits: [ "plums", "kiwis", "oranges", "bananas", "apples" ],
vegetables: [ "broccoli", "zucchini", "carrots", "onions" ]
}
] )

다음 작업은 다음을 제거합니다.

  • "apples"fruits 배열에서 "oranges" 을 반환합니다.

  • "carrots" 배열에서 vegetables

db.stores.updateMany(
{ },
{ $pull: { fruits: { $in: [ "apples", "oranges" ] }, vegetables: "carrots" } }
)

db.collection.find() 를 사용하여 결과를 확인하십시오.

{
_id: 1,
fruits: [ 'pears', 'grapes', 'bananas' ],
vegetables: [ 'celery', 'squash' ]
},
{
_id: 2,
fruits: [ 'plums', 'kiwis', 'bananas' ],
vegetables: [ 'broccoli', 'zucchini', 'onions' ]
}

profiles 컬렉션을 생성합니다.

db.profiles.insertOne( { _id: 1, votes: [ 3, 5, 6, 7, 7, 8 ] } )

다음 작업은 votes 배열에서 ( $gte ) 6보다 크거나 같은 모든 항목을 제거합니다:

db.profiles.updateOne( { _id: 1 }, { $pull: { votes: { $gte: 6 } } } )

업데이트 작업 후 문서에 6보다 작은 값만 남게 됩니다:

{ _id: 1, votes: [ 3, 5 ] }

다음 db.collection.bulkWrite() 작업:

  • profilesBulkWrite 컬렉션을 생성합니다.

  • votes 0} 배열에서 ( $gte ) 6 보다 크거나 같은 모든 항목을 제거합니다.

  • votes 0} 배열에서 ( $lte ) 3 보다 작거나 같은 모든 항목을 제거합니다.

try {
db.profilesBulkWrite.bulkWrite( [
{
insertOne: {
"document": { _id: 1, votes: [ 3, 5, 6, 7, 7, 8 ] }
}
},
{
updateOne: {
"filter": { _id: 1 },
"update": { $pull: { votes: { $gte: 6 } } }
}
},
{
updateOne: {
"filter": {_id: 1},
"update": { $pull: { votes: { $lte: 3 } } }
}
}
] );
} catch (e) {
print(e);
}

참고

bulkWrite()를 호출합니다.

db.collection.bulkWrite() 메소드는 배열에 나열된 여러 쓰기 작업을 실행합니다. 이 예에서 db.collection.bulkWrite()profiles 컬렉션에 대해 여러 작업을 수행합니다.

2} 작업 후 다음 작업을 사용하여 문서에 6보다 작고 3보다 큰 값만 있는지 확인할 수 있습니다:db.collection.bulkWrite()

db.profilesBulkWrite.find()

이 연산은 다음을 반환합니다:

[ { _id: 1, votes: [ 5 ] } ]

survey 컬렉션을 생성합니다.

db.survey.insertMany([
{
_id: 1,
results: [
{ item: "A", score: 5 },
{ item: "B", score: 8 }
]
},
{
_id: 2,
results: [
{ item: "C", score: 8 },
{ item: "B", score: 4 }
]
}
] )

다음 작업은 8score 필드와 "B"item 필드를 모두 포함하는 results 배열에서 모든 요소를 제거합니다.

db.survey.updateMany(
{ },
{ $pull: { results: { score: 8 , item: "B" } } }
)

$pull 표현식은 최상위 문서인 것처럼 results 결과 배열의 각 요소에 조건을 적용합니다.

작업 후 results 배열에는 8score 필드와 "B"item 필드를 모두 포함하는 문서가 포함되지 않습니다.

{ _id: 1, results: [ { item: 'A', score: 5 } ] },
{
_id: 2,
results: [ { item: 'C', score: 8 }, { item: 'B', score: 4 } ]
}

$pull 연산자는 각 요소를 최상위 객체로 취급합니다. 쿼리는 각 요소에 적용됩니다. 표현식은 일치 조건을 지정하기 위해 $elemMatch를 사용할 필요가 없습니다.

반대로 다음 작업은 원래 컬렉션의 어떤 요소도 $pull 처리하지 않습니다.

db.survey.updateMany(
{ },
{ $pull: { results: { $elemMatch: { score: 8 , item: "B" } } } }
)

참고

survey 0} 컬렉션을 삭제합니다:

그런 다음 다시 생성하여 이 예시를 실행합니다.

중첩 배열에 포함된 문서가 포함된 새 survey 컬렉션을 만듭니다.

db.survey.drop()
db.survey.insertMany( [
{
_id: 1,
results: [
{
item: "A",
score: 5,
answers: [ { q: 1, a: 4 }, { q: 2, a: 6 } ]
},
{
item: "B",
score: 8,
answers: [ { q: 1, a: 8 }, { q: 2, a: 9 } ]
}
]
},
{
_id: 2,
results: [
{
item: "C",
score: 8,
answers: [ { q: 1, a: 8 }, { q: 2, a: 7 } ]
},
{
item: "B",
score: 4,
answers: [ { q: 1, a: 0 }, { q: 2, a: 8 } ]
}
]
}
] )

그런 다음 $elemMatch포함된 answers 배열의 요소에 여러 조건을 지정할 수 있습니다:

db.survey.updateMany(
{ },
{
$pull:
{
results:
{
answers: { $elemMatch: { q: 2, a: { $gte: 8 } } }
}
}
}
)

작업은 일치하는 각 문서에서 results 배열을 업데이트했습니다. 포함된 answers 배열의 요소가 강조 표시된 줄의 선택 조건과 일치하면 db.collection.updateMany() results 에서 문서를 제거했습니다.

{
_id: 1,
results: [
{
item: 'A',
score: 5,
answers: [ { q: 1, a: 4 }, { q: 2, a: 6 } ]
}
]
},
{
_id: 2,
results: [
{
item: 'C',
score: 8,
answers: [ { q: 1, a: 8 }, { q: 2, a: 7 } ]
}
]
}

다음도 참조하세요.

돌아가기

$pop

다음

$push

이 페이지의 내용