我无法在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");
但是它没有给我想要的结果。
任何帮助将不胜感激!
投影操作代码:
String [] TIME_ARRAY = { "2020-02-20", "2020-02-21", "2020-02-22" }; // values from params.timeArray
project("_id", "data")
.andArrayOf(TIME_ARRAY).as("timeArray")
.andArrayOf().as("emptyArray"),
project()
.and(
Map.itemsOf("timeArray")
.as("element")
.andApply(ctx ->
new Document("key", "$$element")
.append("value",
new Document("$cond",
Arrays.asList(
new Document("$in",
Arrays.asList("$$element", "$data.date")),
"$data",
"$emptyArr")
)
)
)
)
.as("data")