I'm using spring to run an agreegation query on mongodb. i would add some filter for my projection. so i want to write the flow code using spring
db.mycollection.aggregate( [
{$match: {"_id" : { "$binary" : "sU7XGDnFYz53KHVhP+sZlQ==", "$type" : "03" } }},
{$project: {
myarray: {
$filter: {
input: "$myarray",
as: "item",
cond: { $in: [ "$$item._id", ["JshGyMImCsSceiPqqCCinlAtTrIkwvlx", "0000022211"] ] }
}
},
"colomn" :1,
myarray2: {
$filter: {
input: "$myarray2",
as: "item",
cond: { $in: [ "$$item.name", ["name1", "name2"] ] }
}
}
}
}
] );
My attempt writing with Spring Mongo
/*<arrayname,list ids>*/
Map<String, List<Object>> arraysAggregationMap = new HashMap<>();
...
ProjectionOperation projectionOperation = project();
//adding projections
for (String f: fields) {
projectionOperation = projectionOperation.and(f).as(f);
}
// filters
for (Entry<String, List<Object>> entry: arraysAggregationMap.entrySet()
) {
projectionOperation = projectionOperation.and(filter(entry.getKey()).as("item").by(
in("item"+"._id",entry.getValue()).toString())).as(entry.getKey());
}
Aggregation aggregation;
if(fields.size()>0 ||arraysAggregationMap.size() >0) aggregation = newAggregation(match(creteria),projectionOperation);
else aggregation = newAggregation(match(creteria));
AggregationResults results = mongoOperation.aggregate(aggregation,entityClass,Document.class);
But the $filter
is not working the same.