포함하지 않으려는 필드를 0으로 설정합니다. 이렇게 하면 find()를 사용하는 동안 나머지 값이 표시됩니다. 먼저 문서로 컬렉션을 생성해 보겠습니다. −
> db.demo237.insertOne({
... _id:101,
... Product: {
... description1: {id:1001 },
... description2: {Name:"Product-1" },
... description3: {Price:550 }
... }
...}
...);
{ "acknowledged" : true, "insertedId" : 101 } find() 메서드를 사용하여 컬렉션의 모든 문서 표시 -
> db.demo237.find().pretty();
이것은 다음과 같은 출력을 생성합니다 -
{
"_id" : 101,
"Product" : {
"description1" : {
"id" : 1001
},
"description2" : {
"Name" : "Product-1"
},
"description3" : {
"Price" : 550
}
}
} 다음은 프로젝션을 통해 중첩 문서에서 단일 필드를 제외한 모든 것을 제거하는 쿼리입니다 -
> db.demo237.find({}, { "Product.description1": 0, "Product.description3": 0 }); 이것은 다음과 같은 출력을 생성합니다 -
{ "_id" : 101, "Product" : { "description2" : { "Name" : "Product-1" } } }