【发布时间】:2020-07-01 09:20:23
【问题描述】:
我无法在 ProjectionOperation 的 java mongodb 中执行 map 和 reduce 操作。
我在原生 mongo query 有这样的项目操作:
{
$project: {
data: {
$map: {
input: params.timeArray,
in: {
"key": "$$this",
"value": { "$cond": [{ "$in": ["$$this", "$data.date"] }, "$data", []] }
}
}
}
}
}
我还想在另一个投影操作中做reduce操作,如下所示:
{
$project: {
id: 1,
data: { $reduce: { input: "$data", initialValue: [], in: { $concatArrays: ["$$value", "$$this"] } } }
}
}
我试过这个:
ProjectionOperation projectionOperation1 = Aggregation.project()
.and(VariableOperators.mapItemsOf("data"/*here, I want to pass timeArray*/)
.as("input")
.andApply(context -> new BasicDBObject("value",
ConditionalOperators.when(where("$$this").in("$data.date"))
.then("$data")
.otherwise(new HashSet<>()))))
.as("data");
但这并没有给我想要的结果。
我正在使用 springboot 版本 1.5.10.RELEASE
任何帮助将不胜感激!
【问题讨论】:
-
您是否尝试使用
@Query注释在您的Repository中编写查询? -
请告诉
data这个字段是什么样子的;发布一个示例值。 -
["2020-02-20","2020-02-21","2020-02-22",...] 或 ["2020-01","2020-02" ,"2020-03",...] 或 ["2018","2019","2020",...](字符串数组列表)。
-
你使用的是
$data.date,我认为是这个数组)。 -
不清楚你在说什么。您的聚合显示对象
data中的字段date是一个数组(请参阅我之前的评论)。因此在$map的代码{ "$in": [ "$$this", "$data.date" ] }中引用了“$data.date”。请确认。
标签: java mongodb mapreduce mongodb-query spring-data-mongodb