[英]Aggregation pipeline in spring mongodb
我需要使用spring data mongo db创建聚合plepleine。 我正在使用下面的代码来过滤对象数组。 它工作得很好
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")
但对于其他用例,我需要添加两个条件。 在上面的“ 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" ) ] }
] }
}
原始的MongoDb $ Project阶段如下图所示。我试图在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" ) ] }
] }
}
}
}
我该怎么做 ???
您可以尝试以下片段吗? 我无法在mongo实例上运行它,但是从语法上(希望)在逻辑上是正确的。
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"));
我使用了$filter
支持,在这里详细介绍了DATAMONGO-1491
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.