Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

mongodb - Create filter aggregation in spring


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"]}
                                }}
                            }}
                        ])

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();

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can try below query.

Static Imports

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;

Code

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...