在Spring中创建过滤器聚合 [英] Create filter aggregation in spring

查看:87
本文介绍了在Spring中创建过滤器聚合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我最近开始使用SpringData探索MongoDB中的Aggregation Framework. 我可以创建以下查询,即


I recently started exploring Aggregation Framework in MongoDB with SpringData. I could Create following query i.e

db.consumer_order.aggregate([
                            { $match: {_id: ObjectId("59e43f542397a00de0c688e4"), "orderState":"Confirmed"}},
                            { $project: {
                                parts: {$filter: {
                                    input: '$parts',
                                    as: 'item',
                                    cond: {$eq: ['$$item.currentState', "Estimation Confirmed"]}
                                }}
                            }}
                        ])

在Spring中使用MongoDB本机驱动程序并使用以下代码

with MongoDB Native Driver in Spring with the following Code

List<Document> aggrigationList = new ArrayList<>();

List<String> conditions = new ArrayList<>();
conditions.add("$$item.currentState");
conditions.add("Estimation Confirmed");

Document matchDoc = new Document("$match",new Document("_id",new ObjectId(orderId)));
Document projectDoc = new Document("$project",new Document("parts",new Document("$filter",new Document("input","$parts").append("as", "item").append("cond", new Document("$eq",conditions)))));
aggrigationList.add(matchDoc);
aggrigationList.add(projectDoc);

Document orderWithPendingParts = consumerOrderCollection.aggregate(aggrigationList).first();

但是我确实知道总是与Native Driver一起工作不是一个好习惯,因为我们已经掌握了Spring-Data.但是我在使用Spring Data将上面的MongoDB查询构造到AggrigationObject时遇到了麻烦.我尝试了以下操作,但是在构造 Aggregation.project()即发现困难

But I do know it's not a good practice to work always with Native Driver since we've Spring-Data in hand. But I'm having trouble to construct the above MongoDB query to AggrigationObject Using Spring Data. I tried with the below, But find difficulty in Constructing Aggregation.project() i.e

mongoTemplate.aggregate(Aggregation.newAggregation(
            Aggregation.match(Criteria.where("owner").is(user).andOperator(Criteria.where("orderState").is("confirmed"))),
            ***Finding Difficulty in here***
            ), inputType, outputType)

指导我,如果我做错了事.

Guide me, If I'm doing something wrong.

推荐答案

您可以尝试以下查询.

静态导入

import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;
import static org.springframework.data.mongodb.core.aggregation.ArrayOperators.Filter.filter;
import static org.springframework.data.mongodb.core.aggregation.ComparisonOperators.Eq.valueOf;

代码

Aggregation aggregation = newAggregation(
           project().and(filter("parts")
             .as("item")
             .by(valueOf(
                  "item.currentState")
                   .equalToValue(
                  "Estimation Confirmed")))
          .as("parts");
);

List<outputType> results = mongoTemplate.aggregate(aggregation, inputType, outputType)

这篇关于在Spring中创建过滤器聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆