【问题标题】:How to project map & reduce in ProjectionOperation in Spring Data MongoDB?如何在 Spring Data MongoDB 的 ProjectionOperation 中投影 map 和 reduce?
【发布时间】: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


【解决方案1】:

使用Spring MongoTemplate API运行的投影操作代码:

MongoOperations mongoOps = new MongoTemplate(MongoClients.create(), "test");

String [] TIME_ARRAY = { "2020-02-20", "2020-02-21", "2020-02-22" }; // values from params.timeArray

Aggregation agg = newAggregation(
  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", 
                                       "$emptyArray")
                        )
                    ) 
                )
            )
        .as("data")
);

AggregationResults<Document> results = mongoOps.aggregate(agg, "collection", Document.class);

results.forEach(doc -> System.out.println(doc.toJson()));

【讨论】:

  • 我试过了,但它不起作用。它显示如下错误:“无法解析方法'newAggregation(?,org.springframework.data.mongodb.core.aggregation.ProjectionOperation)'”和“无法解析'ProjectionOperation'中的方法'andArrayOf'”。
  • 这是一个编译错误。你在使用 IDE 吗?如果是这样,您将能够轻松地纠正它(IDE​​ 将能够指导您)。我发布的代码编译并运行良好。
  • 链接到andArrayOf
  • 它显示项目没有方法“andArrayOf”。
  • 这里是它的链接(上一个链接错误!):andArrayOf
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-22
  • 1970-01-01
  • 1970-01-01
  • 2018-03-05
  • 1970-01-01
  • 1970-01-01
  • 2016-02-23
相关资源
最近更新 更多