[英]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.