如何在Spring Data MongoDB中投影地图并在ProjectionOperation中进行缩减?

问题描述 投票:1回答:1

我无法在ProjectionOperation的Java mongodb中执行mapreduce操作。

我在本机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");

但是它没有给我想要的结果。

任何帮助将不胜感激!

java mongodb mapreduce mongodb-query spring-data-mongodb
1个回答
0
投票

投影操作代码:

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")
© www.soinside.com 2019 - 2024. All rights reserved.