정의
구문
$unset
단계의 구문은 다음과 같습니다.
고려 사항
$unset
개인정보 정책에 $project
$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" } }
최상위 필드 제거
다음 예에서는 최상위 필드 isbn
및 copies
를 제거합니다.
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;