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

$unset (aggregation)

이 페이지의 내용

  • 정의
  • 구문
  • 고려 사항
  • 예제

참고

명확화

다음 페이지는 애그리게이션 단계 $unset 를 나타냅니다. 업데이트 연산자 $unset 에 대해서는 $unset 를 참조하세요.

$unset

문서에서 필드를 제거/제외합니다.

$unset 단계의 구문은 다음과 같습니다.

  • 단일 필드를 제거하기 위해 $unset은 제거할 필드를 지정하는 문자열을 취합니다.

    { $unset: "<field>" }
  • 여러 필드를 제거하려면 $unset는 제거할 필드의 배열을 받습니다.

    { $unset: [ "<field1>", "<field2>", ... ] }

$unset은 필드를 제거/제외하는 $project 단계의 별칭입니다.

{ $project: { "<field1>": 0, "<field2>": 0, ... } }

내장된 문서 내에서 필드를 제거하거나 제외하려면 다음과 같이 점 표기법을 사용하면 됩니다.

{ $unset: "<field.nestedfield>" }

또는

{ $unset: [ "<field1.nestedfield>", ...] }

다음 문서로 샘플 books 컬렉션을 생성합니다.

db.books.insertMany([
{ "_id" : 1, title: "Antelope Antics", isbn: "0001122223334", author: { last:"An", first: "Auntie" }, copies: [ { warehouse: "A", qty: 5 }, { warehouse: "B", qty: 15 } ] },
{ "_id" : 2, title: "Bees Babble", isbn: "999999999333", author: { last:"Bumble", first: "Bee" }, copies: [ { warehouse: "A", qty: 2 }, { warehouse: "B", qty: 5 } ] }
])

다음 예에서는 최상위 필드 copies를 제거합니다.

db.books.aggregate([ { $unset: "copies" } ])

또는 다음 구문을 사용할 수도 있습니다.

db.books.aggregate([ { $unset: [ "copies" ] } ])

두 작업 모두 다음 문서를 반환합니다.

{ "_id" : 1, "title" : "Antelope Antics", "isbn" : "0001122223334", "author" : { "last" : "An", "first" : "Auntie" } }
{ "_id" : 2, "title" : "Bees Babble", "isbn" : "999999999333", "author" : { "last" : "Bumble", "first" : "Bee" } }

다음 예에서는 최상위 필드 isbncopies를 제거합니다.

db.books.aggregate([
{ $unset: [ "isbn", "copies" ] }
])

$unset 연산은 다음 문서를 출력합니다.

{ "_id" : 1, "title" : "Antelope Antics", "author" : { "last" : "An", "first" : "Auntie" } }
{ "_id" : 2, "title" : "Bees Babble", "author" : { "last" : "Bumble", "first" : "Bee" } }

다음 예시에서는 최상위 필드 isbn, 내장된 필드를 first (name 문서에서) 제거하고 및 내장된 필드 warehouse를(copies 배열의 요소에서) 제거합니다.

db.books.aggregate([
{ $unset: [ "isbn", "author.first", "copies.warehouse" ] }
])

$unset 연산은 다음 문서를 출력합니다.

{ "_id" : 1, "title" : "Antelope Antics", "author" : { "last" : "An" }, "copies" : [ { "qty" : 5 }, { "qty" : 15 } ] }
{ "_id" : 2, "title" : "Bees Babble", "author" : { "last" : "Bumble" }, "copies" : [ { "qty" : 2 }, { "qty" : 5 } ] }
← $unionWith (aggregation)

이 페이지의 내용