개요
이 가이드 에서는 프로젝션 을 사용하여 읽기 작업에서 반환할 필드를 지정하기 위해 C++ 운전자 를 사용하는 방법을 학습 수 있습니다. 프로젝션 은 MongoDB 가 쿼리 에서 반환하는 필드를 지정하는 문서 입니다.
샘플 데이터
The examples in this guide use the restaurants collection in the sample_restaurants database from the Atlas sample datasets. To access this collection from your C++ application, instantiate a mongocxx::client that connects to an Atlas cluster and assign the following values to your db and collection variables:
auto db = client["sample_restaurants"]; auto collection = db["restaurants"];
무료 MongoDB Atlas 클러스터 생성하고 샘플 데이터 세트를 로드하는 방법을 학습하려면 MongoDB 시작하기 가이드 를 참조하세요.
프로젝션 유형
프로젝션을 사용하여 반환 문서에 포함할 필드를 지정하거나 제외할 필드를 지정할 수 있습니다. _id 필드를 제외하지 않는 한 포함 및 제외 문을 단일 프로젝션에서 결합할 수 없습니다.
포함할 필드 지정
결과에 포함할 필드를 지정하려면 mongocxx::options::find 클래스의 인스턴스 를 만들고 해당 projection 필드 를 설정하다 합니다. 이 필드 를 설정하다 하려면 다음 구문을 사용합니다.
<options instance>.projection(make_document(kvp("<field name>", 1)));
다음 예시 에서는 일치하는 문서의 name, cuisine 및 borough 필드만 반환하도록 mongocxx::options::find 객체 의 projection 필드 를 설정합니다. 그런 다음 find() 메서드를 호출하여 name 필드 값이 "Emerald Pub" 인 모든 레스토랑을 찾고 mongocxx::options::find 객체 를 find() 에 매개 변수로 전달합니다.
mongocxx::options::find opts{}; opts.projection(make_document(kvp("name", 1), kvp("cuisine", 1), kvp("borough", 1))); auto cursor = collection.find(make_document(kvp("name", "Emerald Pub")), opts); for(auto&& doc : cursor) { std::cout << bsoncxx::to_json(doc) << std::endl; }
{ "_id" : { "$oid" : "..." }, "borough" : "Manhattan", "cuisine" : "American", "name" : "Emerald Pub" } { "_id" : { "$oid" : "..." }, "borough" : "Queens", "cuisine" : "American", "name" : "Emerald Pub" }
When you use a projection to specify fields to include in the return document, the _id field is also included by default. All other fields are implicitly excluded. To remove the _id field from the return document, you must explicitly exclude it.
_id 필드 제외
포함할 필드를 지정할 때 반환된 문서에서 _id 필드를 제외할 수도 있습니다.
다음 예시 에서는 이전 예시 와 동일한 쿼리 를 수행하지만 프로젝션 에서 _id 필드 를 제외합니다.
mongocxx::options::find opts{}; opts.projection(make_document(kvp("_id", 0), kvp("name", 1), kvp("cuisine", 1), kvp("borough", 1))); auto cursor = collection.find(make_document(kvp("name", "Emerald Pub")), opts); for(auto&& doc : cursor) { std::cout << bsoncxx::to_json(doc) << std::endl; }
{ "borough" : "Manhattan", "cuisine" : "American", "name" : "Emerald Pub" } { "borough" : "Queens", "cuisine" : "American", "name" : "Emerald Pub" }
제외할 필드 지정
결과에서 제외할 필드를 지정하려면 mongocxx::options::find 클래스의 인스턴스 를 만들고 해당 projection 필드 를 설정하다 합니다. 이 필드 를 설정하다 하려면 다음 구문을 사용합니다.
<options instance>.projection(make_document(kvp("<field name>", 0)));
다음 예시 에서는 일치하는 문서의 grades 및 address 필드를 제외하도록 mongocxx::options::find 객체 의 projection 필드 를 설정합니다. 그런 다음 find() 메서드를 호출하여 name 필드 값이 "Emerald Pub" 인 모든 레스토랑을 찾고 mongocxx::options::find 객체 를 find() 에 매개 변수로 전달합니다.
mongocxx::options::find opts{}; opts.projection(make_document(kvp("grades", 0), kvp("address", 0))); auto cursor = collection.find(make_document(kvp("name", "Emerald Pub")), opts); for(auto&& doc : cursor) { std::cout << bsoncxx::to_json(doc) << std::endl; }
{ "_id" : { "$oid" : "..." }, "borough" : "Manhattan", "cuisine" : "American", "name" : "Emerald Pub", "restaurant_id" : "40367329" } { "_id" : { "$oid" : "..." }, "borough" : "Queens", "cuisine" : "American", "name" : "Emerald Pub", "restaurant_id" : "40668598" }
프로젝션을 사용하여 제외할 필드를 지정하면 지정되지 않은 모든 필드가 반환 문서에 암시적으로 포함됩니다.
추가 정보
To learn more about projections, see the Project Fields guide in the MongoDB Server manual.
API 문서
이 가이드에서 사용되는 메서드 또는 유형에 대해 자세히 알아보려면 다음 API 설명서를 참조하세요.