投影构建器
概述
在本指南中,您可以学习如何使用MongoDB Java驱动中的构建器指定投影。
MongoDB支持字段投影,指定返回查询结果时要包含和排除的字段。MongoDB中的投影遵循一些基本规则
的
_id
字段始终包含,除非明确排除指定字段以包含时,隐式排除除
_id
字段之外的所有其他字段指定字段以排除时,仅移除查询结果中的该字段
有关投影机制的更多信息,请在此处查看。.
Projections
类为所有MongoDB投影运算符提供静态工厂方法。每个方法都返回一个BSON类型的实例,您可以将其传递给任何期望投影的方法。
提示
示例文档和示例
以下部分包含对名为 projection_builders
的示例集合执行的查询和投影操作的示例。每个部分都使用名为 collection
的变量来引用 projection_builders
集合的 MongoCollection
实例。
该集合包含以下文档,表示 2018 年和 2019 年的月平均气温(摄氏度)
{ "year" : 2018, "type" : "even number but not a leap year", "temperatures" : [ { "month" : "January", "avg" : 9.765 }, { "month" : "February", "avg" : 9.675 }, { "month" : "March", "avg" : 10.004 }, { "month" : "April", "avg" : 9.983 }, { "month" : "May", "avg" : 9.747 }, { "month" : "June", "avg" : 9.65 }, { "month" : "July", "avg" : 9.786 }, { "month" : "August", "avg" : 9.617 }, { "month" : "September", "avg" : 9.51 }, { "month" : "October", "avg" : 10.042 }, { "month" : "November", "avg" : 9.452 }, { "month" : "December", "avg" : 9.86 } ] }, { "year" : 2019, "type" : "odd number, can't be a leap year", "temperatures" : [ { "month" : "January", "avg" : 10.023 }, { "month" : "February", "avg" : 9.808 }, { "month" : "March", "avg" : 10.43 }, { "month" : "April", "avg" : 10.175 }, { "month" : "May", "avg" : 9.648 }, { "month" : "June", "avg" : 9.686 }, { "month" : "July", "avg" : 9.794 }, { "month" : "August", "avg" : 9.741 }, { "month" : "September", "avg" : 9.84 }, { "month" : "October", "avg" : 10.15 }, { "month" : "November", "avg" : 9.84 }, { "month" : "December", "avg" : 10.366 } ] }
投影操作
以下部分包含有关可用投影操作以及如何使用 Projections
类构建它们的信息。
包含
使用 include()
方法指定一个或多个字段的包含。
以下示例包含 year
字段以及(隐含地)_id
字段
Bson filter = Filters.empty(); Bson projection = include("year"); collection.find(filter).projection(projection).forEach(doc -> System.out.println(doc.toJson()));
以下代码显示了此投影的输出
{"_id": {"$oid": "6042edc9f2b56342164e0790"}, "year": 2018} {"_id": {"$oid": "6042edc9f2b56342164e0791"}, "year": 2019}
以下示例包含 year
和 type
字段以及(隐含地)_id
字段
Bson filter = Filters.empty(); Bson projection = include("year", "type"); collection.find(filter).projection(projection).forEach(doc -> System.out.println(doc.toJson()));
以下代码显示了此投影的输出
{"_id": {"$oid": "6042edc9f2b56342164e0790"}, "year": 2018, "type": "even number but not a leap year"} {"_id": {"$oid": "6042edc9f2b56342164e0791"}, "year": 2019, "type": "odd number, can't be a leap year"}
排除
使用exclude()
方法指定排除一个或多个字段。
以下示例排除了temperatures
字段
Bson filter = Filters.empty(); Bson projection = exclude("temperatures"); collection.find(filter).projection(projection).forEach(doc -> System.out.println(doc.toJson()));
以下代码显示了此投影的输出
{"_id": {"$oid": "6042edc9f2b56342164e0790"}, "year": 2018, "type": "even number but not a leap year"} {"_id": {"$oid": "6042edc9f2b56342164e0791"}, "year": 2019, "type": "odd number, can't be a leap year"}
以下示例排除了type
和temperatures
字段
Bson filter = Filters.empty(); Bson projection = exclude("temperatures", "type"); collection.find(filter).projection(projection).forEach(doc -> System.out.println(doc.toJson()));
以下代码显示了此投影的输出
{"_id": {"$oid": "6042edc9f2b56342164e0790"}, "year": 2018} {"_id": {"$oid": "6042edc9f2b56342164e0791"}, "year": 2019}
组合投影
使用fields()
方法组合多个投影。
以下示例包括year
和type
字段,并排除_id
字段
Bson filter = Filters.empty(); Bson projection = fields(include("year", "type"), exclude("_id")); collection.find(filter).projection(projection).forEach(doc -> System.out.println(doc.toJson()));
以下代码显示了此投影的输出
{"year": 2018, "type": "even number but not a leap year"} {"year": 2019, "type": "odd number, can't be a leap year"}
_id
的排除
使用excludeId()
便捷方法指定排除_id
字段
Bson filter = Filters.empty(); Bson projection = fields(include("year", "type"), excludeId()); collection.find(filter).projection(projection).forEach(doc -> System.out.println(doc.toJson()));
以下代码显示了此投影的输出
{"year": 2018, "type": "even number but not a leap year"} {"year": 2019, "type": "odd number, can't be a leap year"}
投影数组元素匹配
使用 elemMatch(String, Bson)
方法变体来指定一个数组投影,该投影将包括与提供的查询过滤器匹配的数组的第一元素。此过滤操作在检索到匹配查询过滤器(如果提供)的所有文档之后执行。
注意
仅包括与指定查询过滤器匹配的第一个元素,无论可能有多少匹配项。
以下示例投影了 temperatures
数组中平均值(avg
)大于 10.1
的第一个元素
Bson filter = Filters.empty(); Bson projection = fields(include("year"), elemMatch("temperatures", Filters.gt("avg", 10.1))); collection.find(filter).projection(projection).forEach(doc -> System.out.println(doc.toJson()));
以下代码显示了此投影的输出
{"_id": {"$oid": "6042edc9f2b56342164e0790"}, "year": 2018} {"_id": {"$oid": "6042edc9f2b56342164e0791"}, "year": 2019, "temperatures": [{"month": "March", "avg": 10.43}]}
当您在操作的 查询 部分指定匹配标准时,请使用 elemMatch(String)
方法变体来指定一个 位置投影,以包括数组的第一个元素。仅检索与查询过滤器匹配的文档。
重要
在 MongoDB 版本 < 4.4 中,指定的数组字段必须出现在查询过滤器中。从 MongoDB 4.4 开始,您可以对未出现在查询过滤器中的数组字段使用位置投影。
以下示例投影了 temperatures
数组的第一个元素
Bson filter = Filters.gt("temperatures.avg", 10.1); Bson projection = fields(include("year"), elemMatch("temperatures")); collection.find(filter).projection(projection).forEach(doc -> System.out.println(doc.toJson()));
以下代码显示了此投影的输出
{"_id": {"$oid": "6042edc9f2b56342164e0791"}, "year": 2019, "temperatures": [{"month": "March", "avg": 10.43}]}
投影数组切片
使用 slice()
方法来投影数组的 切片。
以下示例投影了 temperatures
数组的第一个 6 个元素
Bson filter = Filters.empty(); // first half of the year Bson projection = slice("temperatures", 6); collection.find(filter).projection(projection) .forEach(doc -> System.out.println(doc.toJson(JsonWriterSettings.builder().indent(true).build())));
以下代码显示了此投影的输出
{ "_id": { "$oid": "6042f1bc8ee6fa2a84d2be69" }, "year": 2018, "type": "even number but not a leap year", "temperatures": [ ... <January-June temperature nested documents> ] } { "_id": { "$oid": "6042f1bc8ee6fa2a84d2be6a" }, "year": 2019, "type": "odd number, can't be a leap year", "temperatures": [ ... <January-June temperature nested documents> ] }
以下示例跳过了 temperatures
数组的第一个 6 个元素,并投影下一个 6 个
Bson filter = Filters.empty(); // second half of the year Bson projection = slice("temperatures", 6, 6); collection.find(filter).projection(projection) .forEach(doc -> System.out.println(doc.toJson(JsonWriterSettings.builder().indent(true).build())));
以下代码显示了此投影的输出
{ "_id": { "$oid": "6042f1bc8ee6fa2a84d2be69" }, "year": 2018, "type": "even number but not a leap year", "temperatures": [ ... <July-December temperature nested documents> ] } { "_id": { "$oid": "6042f1bc8ee6fa2a84d2be6a" }, "year": 2019, "type": "odd number, can't be a leap year", "temperatures": [ ... <July-December temperature nested documents> ] }
将文本得分投影为
使用metaTextScore()
方法指定文本查询的得分投影
以下示例将文本得分投影为score
字段的值
Bson filter = Filters.text("even number"); Bson projection = fields(include("year"), metaTextScore("score")); collection.find(filter).projection(projection).forEach(doc -> System.out.println(doc.toJson()));
以下代码显示了此投影的输出
{"_id": {"$oid": "6042f1bc8ee6fa2a84d2be69"}, "year": 2018, "score": 1.25} {"_id": {"$oid": "6042f1bc8ee6fa2a84d2be6a"}, "year": 2019, "score": 0.625}