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.
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.
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.
A TypedAggregation is a special Aggregation that holds information of the input aggregation type.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With