투영은 선택한 필드만 표시되어야 함을 의미합니다. 표시하려면 필드를 1로 설정하십시오.
먼저 문서로 컬렉션을 만들어 보겠습니다. −
> db.demo384.insertOne({"StudentName":"Chris Brown","StudentCountryName":"US"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e5b67a022064be7ab44e7f2")
}
> db.demo384.insertOne({"StudentName":"David Miller","StudentCountryName":"AUS"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e5b67ab22064be7ab44e7f3")
}
> db.demo384.insertOne({"StudentName":"John Doe","StudentCountryName":"UK"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e5b67b422064be7ab44e7f4")
} find() 메서드를 사용하여 컬렉션의 모든 문서 표시 -
> db.demo384.find();
이것은 다음과 같은 출력을 생성합니다 -
{ "_id" : ObjectId("5e5b67a022064be7ab44e7f2"), "StudentName" : "Chris Brown", "StudentCountryName" : "US" }
{ "_id" : ObjectId("5e5b67ab22064be7ab44e7f3"), "StudentName" : "David Miller", "StudentCountryName" : "AUS" }
{ "_id" : ObjectId("5e5b67b422064be7ab44e7f4"), "StudentName" : "John Doe", "StudentCountryName" : "UK" } 다음은 단일 필드만 표시하고 나머지는 무시하는 쿼리입니다 -
> db.demo384.find({},{_id:0,StudentName:0}); 이것은 다음과 같은 출력을 생성합니다 -
{ "StudentCountryName" : "US" }
{ "StudentCountryName" : "AUS" }
{ "StudentCountryName" : "UK" }