Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 679
Kishore Kumar Korada Avatar asked Oct 16 '17 10:10

Kishore Kumar Korada


People also ask

How do I filter an array in MongoDB aggregation?

Filter MongoDB Array Element Using $Filter Operator This operator uses three variables: input – This represents the array that we want to extract. cond – This represents the set of conditions that must be met. as – This optional field contains a name for the variable that represent each element of the input array.

What is $Not in aggregation?

Definition. Evaluates a boolean and returns the opposite boolean value; i.e. when passed an expression that evaluates to true , $not returns false ; when passed an expression that evaluates to false , $not returns true . For more information on expressions, see Expressions.

What is TypedAggregation?

A TypedAggregation is a special Aggregation that holds information of the input aggregation type.

What is Projectionoperation in MongoDB?

Encapsulates the aggregation framework $project -operation. Projection of field to be used in an Aggregation . A projection is similar to a Field inclusion/exclusion but more powerful. It can generate new fields, change values of given field etc.


1 Answers

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)
like image 84
s7vr Avatar answered Oct 30 '22 13:10

s7vr