Overview
在本指南中,您可以学习如何使用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)));
以下示例将mongocxx::options::find对象的projection字段设置为仅返回匹配文档的name 、 cuisine和borough字段。 然后,它调用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)));
以下示例将mongocxx::options::find对象的projection字段设置为排除匹配文档的grades和address字段。 然后,它调用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 文档: