Docs Menu
Docs Home
/
데이터베이스 매뉴얼
/ / /

$unset (집계)

참고

명확화

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

$unset

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

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

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

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

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

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

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

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

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

or

{ $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 } ] }

이 페이지의 Node.js 예제에서는 sample_mflix Atlas 샘플 데이터 세트의 데이터베이스 사용합니다. 무료 MongoDB Atlas cluster 생성하고 샘플 데이터 세트를 로드하는 방법을 학습 MongoDB Node.js 운전자 설명서에서 시작하기를 참조하세요.

MongoDB Node.js 운전자 사용하여 집계 파이프라인 에 $unset 단계를 추가하려면 파이프라인 객체 에서 $unset 연산자 사용합니다.

다음 예시 반환 문서에서 tomatoes 필드 와 imdb.votes 임베디드 필드 제외하는 파이프라인 단계를 만듭니다. 그런 다음 이 예시 에서는 집계 파이프라인 실행합니다.

const pipeline = [{ $unset: ["imdb.votes", "tomatoes"] }];
const cursor = collection.aggregate(pipeline);
return cursor;

돌아가기

$unionWith

이 페이지의 내용