[英]In MongoDB Java Driver how do you use $filter
我有一个适用于 MQL 的查询。 我需要将其翻译成 Java。MQL 中的查询如下所示
db.<collection>.aggregate( [
{
$project: {
"MonitoringLocationIdentifier": 1,
epochTimes: {
$filter: {
input: "$epochTimes",
as: "epochTime",
cond: { $and: [ {$gte: [ "$$epochTime", NumberLong("0") ]}, {$lte: ["$$epochTime", NumberLong("558268020000")]} ]}
}
},
}
}
] )
该集合包含如下所示的文档
{"_id" : ObjectId("633218dfec534a6fe90106b8"),
"MonitoringLocationIdentifier": "Site1",
"epochTimes" : [ NumberLong("451058760000"), NumberLong("558189720000"), NumberLong("516460860000") ] }
我正在尝试获取集合中的所有文档,但按最小/最大过滤每个文档的“epochTimes”。
那里有 Java 驱动向导吗?
我搜索了mongodb java aggregation
,Google 从他们那里得到了这篇“Java - 聚合管道”文章。 它介绍了一些示例,但也提到了 Compass 工具具有导出管道到特定语言的功能。
因此,在打开 Compass 之后,我使用了CREATE NEW
-> Pipeline from text
并从上方粘贴到您的 MQL 中。 加载后我点击EXPORT TO LANGUAGE
,在下拉列表中选择JAVA
,添加Include Import Statements
并产生以下内容:
import java.util.Arrays;
import org.bson.Document;
Arrays.asList(new Document("$project",
new Document("MonitoringLocationIdentifier", 1L)
.append("epochTimes",
new Document("$filter",
new Document("input", "$epochTimes")
.append("as", "epochTime")
.append("cond",
new Document("$and", Arrays.asList(new Document("$gte", Arrays.asList("$$epochTime", 0L)),
new Document("$lte", Arrays.asList("$$epochTime", 558268020000L)))))))))
(还有一个Include Driver Syntax
按钮,如果需要的话似乎会更加冗长)。
所以我不是 Java 驱动程序向导,但希望这有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.