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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…