I need to create aggregation pipleine using spring data mongo db. I am using below code to filter array of objects. It works perfectly fine
Aggregation aggregation = newAggregation(
match(new Criteria().andOperator(criteriaList.toArray(new Criteria[criteriaList.size()]))),
project().and(new AggregationExpression() {
@Override
public Document toDocument(AggregationOperationContext aggregationOperationContext) {
Document filterExpression = new Document();
filterExpression.put("input", "$dailyVisitorStats");
filterExpression.put("as", "dailyVisitorStat");
filterExpression.put("cond", new Document("$eq", Arrays.asList("$$dailyVisitorStat.weekNum", weekNum)));
return new Document("$filter", filterExpression);
}
}).as("dailyVisitorStats")
but for other use case I need to add two condition. In the above example for "Cond" I need to add condition like
{ $and: [
{ $eq: [ "$$dailyVisitorStat.weekNum", 22 ] },
{ $gte: [ "$$dailyVisitorStat.today", ISODate("2019-06-01T00:00:00.000Z" ) ] },
{ $lte: [ "$$dailyVisitorStat.today", ISODate("2019-06-01T23:59:59.000Z" ) ] }
] }
}
The origional MongoDb $Project stage look like below.. I am trying to write same thing in spring mongo
{
"dailyVisitorStats":{
$filter: {
input: "$dailyVisitorStats",
as: "dailyVisitorStat",
cond: { $and: [
{ $eq: [ "$$dailyVisitorStat.weekNum", 22 ] },
{ $gte: [ "$$dailyVisitorStat.today", ISODate("2019-06-01T00:00:00.000Z" ) ] },
{ $lte: [ "$$dailyVisitorStat.today", ISODate("2019-06-01T23:59:59.000Z" ) ] }
] }
}
}
}
How can I do it ???
Can you try the below snippet? I am unable to run it against a mongo instance, but it is syntactically and (hopefully) logically correct.
Aggregation aggregation = newAggregation(
match(new Criteria().andOperator(criteriaList.toArray(new Criteria[criteriaList.size()]))),
project().and(ArrayOperators.Filter.filter("dailyVisitorStats")
.as("dailyVisitorStat")
.by(BooleanOperators.And.and(
ComparisonOperators.Eq.valueOf("dailyVisitorStat.weekNum").equalTo(weekNum),
ComparisonOperators.Gte.valueOf("dailyVisitorStat.today").greaterThanEqualTo(date1),
ComparisonOperators.Lte.valueOf("dailyVisitorStat.today").lessThanEqualTo(date2))))
.as("dailyVisitorStats"));
I used the $filter
support that is detailed here DATAMONGO-1491
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.