Overview
在本指南中,您可以学习;了解如何使用Rust驾驶员通过投影来指定从读取操作中返回哪些字段。投影是指定MongoDB从查询中返回哪些字段的文档。
样本数据
本指南中的示例使用restaurants sample_restaurantsAtlas示例数据集的 数据库中的 集合。要从Rust应用程序访问权限此集合,请创建一个连接到AtlasClient 集群的 ,并将以下值分配给database 和collection 变量:
let database = client.database("sample_restaurants"); let collection: Collection<Document> = database.collection("restaurants");
要学习如何创建免费的MongoDB Atlas 集群并加载示例数据集,请参阅MongoDB 入门指南。
投影类型
使用投影指定要在返回的文档中包含或排除哪些字段。除非排除 _id字段,否则不能在单个投影中组合包含和排除语句。
指定要包含的字段
要在读取操作结果中包含特定字段,请对 find() 方法调用的结果指定 projection() 方法。将文档传递给 projection() 方法,该方法使用以下语法指定要包含的字段。指定值 1 以在返回的文档中包含字段,指定值 0 以排除字段。
.projection(doc! { "<field_name>": 1, // Includes the field in the returned document "_id": 0 // Excludes the _id field from the returned document })
以下示例使用 find() 方法查找 name字段值为 "Emerald
Pub" 的所有餐厅。然后,代码调用 projection() 方法以仅返回匹配文档的 name、cuisine 和 borough 字段。
选择 Asynchronous或Synchronous标签页,查看每个运行时的相应代码:
let filter = doc! { "name": "Emerald Pub" }; let mut cursor = collection .find(filter) .projection(doc! { "name": 1, "cuisine": 1, "borough": 1 }) .await?; while let Some(result) = cursor.try_next().await? { println!("{:?}", result); }
Document({"_id": ObjectId("..."), "borough": String("Manhattan"), "cuisine": String("American"), "name": String("Emerald Pub")}) Document({"_id": ObjectId("..."), "borough": String("Queens"), "cuisine": String("American"), "name": String("Emerald Pub")})
let filter = doc! { "name": "Emerald Pub" }; let mut cursor = collection .find(filter) .projection(doc! { "name": 1, "cuisine": 1, "borough": 1 }) .run()?; for result in cursor { println!("{:?}", result?); }
Document({"_id": ObjectId("..."), "borough": String("Manhattan"), "cuisine": String("American"), "name": String("Emerald Pub")}) Document({"_id": ObjectId("..."), "borough": String("Queens"), "cuisine": String("American"), "name": String("Emerald Pub")})
使用投影指定要包含在返回文档中的字段时,默认下还包含_id 字段。所有其他字段均会隐式排除。要从返回文档中删除_id 字段,请将其明确排除。
排除 _id 字段
在指定要包含的字段时,您还可以从返回的文档中排除_id字段。
以下示例执行与前面示例相同的查询,但从投影中排除 _id字段。选择 Asynchronous 或 Synchronous标签页,查看每个运行时的相应代码:
let filter = doc! { "name": "Emerald Pub" }; let mut cursor = collection.find(filter) .projection(doc! { "name": 1, "cuisine": 1, "borough": 1, "_id": 0 }) .await?; while let Some(result) = cursor.try_next().await? { println!("{:?}", result); }
Document({"borough": String("Manhattan"), "cuisine": String("American"), "name": String("Emerald Pub")}) Document({"borough": String("Queens"), "cuisine": String("American"), "name": String("Emerald Pub")})
let filter = doc! { "name": "Emerald Pub" }; let mut cursor = collection.find(filter) .projection(doc! { "name": 1, "cuisine": 1, "borough": 1, "_id": 0 }) .run()?; for result in cursor { println!("{:?}", result?); }
Document({"borough": String("Manhattan"), "cuisine": String("American"), "name": String("Emerald Pub")}) Document({"borough": String("Queens"), "cuisine": String("American"), "name": String("Emerald Pub")})
指定要排除的字段
要从读取操作结果中排除特定字段,请对 find() 方法调用的结果指定 projection() 方法。将文档传递给 projection() 方法,该方法使用以下语法指定要排除的字段:
.projection(doc! { "<field_name>": 0 })
以下示例使用 find() 方法查找 name字段值为 "Emerald
Pub" 的所有餐厅。然后,代码调用 projection() 方法以省略结果中的 grades 和 address 字段。选择Asynchronous或Synchronous标签页以查看每个运行时的相应代码:
let filter = doc! { "name": "Emerald Pub" }; let mut cursor = collection.find(filter) .projection(doc! { "grades": 0, "address": 0 }) .await?; while let Some(result) = cursor.try_next().await? { println!("{:?}", result); }
Document({"_id": ObjectId("..."), "borough": String("Manhattan"), "cuisine": String("American"), "name": String("Emerald Pub"), "restaurant_id": String("40367329")}) Document({"_id": ObjectId("..."), "borough": String("Queens"), "cuisine": String("American"), "name": String("Emerald Pub"), "restaurant_id": String("40668598")})
let filter = doc! { "name": "Emerald Pub" }; let mut cursor = collection.find(filter) .projection(doc! { "grades": 0, "address": 0 }) .run()?; for result in cursor { println!("{:?}", result?); }
Document({"_id": ObjectId("..."), "borough": String("Manhattan"), "cuisine": String("American"), "name": String("Emerald Pub"), "restaurant_id": String("40367329")}) Document({"_id": ObjectId("..."), "borough": String("Queens"), "cuisine": String("American"), "name": String("Emerald Pub"), "restaurant_id": String("40668598")})
使用投影指定要排除的字段时,任何未指定的字段都将隐式包含在返回文档中。
更多信息
要学习;了解有关投影的更多信息,请参阅MongoDB Server手册中的“项目字段”指南。
API 文档
要学习;了解有关find() 方法的详情,请参阅 find() API文档。