문서 메뉴

문서 홈애플리케이션 개발MongoDB 매뉴얼

unsetField (집계)

이 페이지의 내용

  • 정의
  • 구문
  • 행동
  • 예제
$unsetField

버전 5.0에 추가.

문서에서 지정된 필드를 제거합니다.

$unsetField 를 사용하여 이름에 마침표(.)가 포함되거나 달러 기호($)로 시작하는 필드를 제거할 수 있습니다.

$unsetField$$REMOVE 를 사용하여 필드를 제거하는 $setField 의 별칭입니다.

$unsetField 의 구문은 다음과 같습니다:

{
$unsetField: {
field: <String>,
input: <Object>,
}
}

다음 필드를 제공해야 합니다.

필드
유형
설명
field
문자열
추가, 업데이트 또는 제거하려는 input 객체의 필드입니다. field는 문자열 상수로 해석되는 모든 유효한 표현식이 될 수 있습니다.
input
개체
추가하거나 업데이트하려는 field가 포함된 문서입니다. input은 객체, missing, null 또는 undefined로 해석되어야 합니다.
  • inputmissing, undefined 또는 null 로 평가되면 $unsetFieldnull 을 반환하고 input 을 업데이트하지 않습니다.

  • input 가 객체, missing, undefined 또는 null 이외의 것으로 평가되는 경우 $unsetField 은 오류를 반환합니다.

  • field 이 문자열 상수 이외의 것으로 해석되면 $unsetField 는 오류를 반환합니다.

  • fieldinput 에 존재하지 않으면 $unsetField 가 이를 추가합니다.

  • $unsetField 은(는) $unsetField 객체나 배열을 암시적으로 순회하지 않습니다. 예를 들어 은 field 값 을 "a.b.c" "a.b.c" 중첩된 필드 대신 최상위 필드 로{ "a": { "b": { "c": } } } 평가합니다.

인벤토리 컬렉션을 가정해 보겠습니다.

db.inventory.insertMany( [
{ _id: 1, item: "sweatshirt", qty: 300, "price.usd": 45.99 },
{ _id: 2, item: "winter coat", qty: 200, "price.usd": 499.99 },
{ _id: 3, item: "sun dress", qty: 250, "price.usd": 199.99 },
{ _id: 4, item: "leather boots", qty: 300, "price.usd": 249.99 },
{ _id: 5, item: "bow tie", qty: 180, "price.usd": 9.99 }
] )

$replaceWith 파이프라인 단계와 $unsetField 연산자를 사용하여 각 문서에서 "price.usd" 필드를 제거합니다.

db.inventory.aggregate( [
{ $replaceWith: {
$unsetField: {
field: "price.usd",
input: "$$ROOT"
} } }
] )

이 연산은 다음과 같은 결과를 반환합니다.

[
{ _id: 1, item: 'sweatshirt', qty: 300 },
{ _id: 2, item: 'winter coat', qty: 200 },
{ _id: 3, item: 'sun dress', qty: 250 },
{ _id: 4, item: 'leather boots', qty: 300 },
{ _id: 5, item: 'bow tie', qty: 180 }
]

인벤토리 컬렉션을 가정해 보겠습니다.

db.inventory.insertMany( [
{ _id: 1, item: "sweatshirt", qty: 300, "$price": 45.99 },
{ _id: 2, item: "winter coat", qty: 200, "$price": 499.99 },
{ _id: 3, item: "sun dress", qty: 250, "$price": 199.99 },
{ _id: 4, item: "leather boots", qty: 300, "$price": 249.99 },
{ _id: 5, item: "bow tie", qty: 180, "$price": 9.99 }
] )

$replaceWith$unsetField 연산자와 함께 $literal 파이프라인 단계를 사용하여 "$price" 각 문서에서 필드를 제거합니다.

db.inventory.aggregate( [
{ $replaceWith: {
$unsetField: {
field: { $literal: "$price" },
input: "$$ROOT"
} } }
] )

이 연산은 다음과 같은 결과를 반환합니다.

[
{ _id: 1, item: 'sweatshirt', qty: 300 },
{ _id: 2, item: 'winter coat', qty: 200 },
{ _id: 3, item: 'sun dress', qty: 250 },
{ _id: 4, item: 'leather boots', qty: 300 },
{ _id: 5, item: 'bow tie', qty: 180 }
]

인벤토리 컬렉션을 가정해 보겠습니다.

db.inventory.insertMany( [
{ _id: 1, item: "sweatshirt", qty: 300, "price": {"usd":45.99, "euro": 38.77 } },
{ _id: 2, item: "winter coat", qty: 200, "price": { "usd": 499.99, "euro": 420.51 } },
{ _id: 3, item: "sun dress", qty: 250, "price": { "usd": 199.99, "euro": 167.70 } },
{ _id: 4, item: "leather boots", qty: 300, "price": { "usd": 249.99, "euro": 210.68 } },
{ _id: 5, item: "bow tie", qty: 180, "price": { "usd": 9.99, "euro": 8.42 } }
] )

"price" 필드에는 "usd""euro"라는 두 개의 하위 필드가 있는 문서가 포함되어 있습니다. "price.euro"를 사용하여 "euro"를 식별하고 제거할 수 없습니다. 왜냐하면 MongoDB는 "price.euro"를 마침표(.)가 포함된 최상위 필드 이름으로 구문 분석하기 때문입니다.

$replaceWith 파이프라인 단계를 $setField 및 중첩된 $unsetField 작업과 함께 사용하여 "euro" 필드를 제거합니다.

db.inventory.aggregate( [
{ $replaceWith: {
$setField: {
field: "price",
input: "$$ROOT",
value: {
$unsetField: {
field: "euro",
input: { $getField: "price" }
} } } } }
] )

이 연산은 다음과 같은 결과를 반환합니다.

[
{ _id: 1, item: "sweatshirt", qty: 300, price: { usd: 45.99 } },
{ _id: 2, item: "winter coat", qty: 200, price: { usd: 499.99 } },
{ _id: 3, item: "sun dress", qty: 250, price: { usd: 199.99 } },
{ _id: 4, item: "leather boots", qty: 300, price: { usd: 249.99 } },
{ _id: 5, item: "bow tie", qty: 180, price: { usd: 9.99 } }
]

다음도 참조하세요.

← $type (애그리게이션)

이 페이지의 내용