这是我的代码(Spring 数据):
MatchOperation matchOperation = Aggregation.match(new Criteria("stats.channelId").is(channelId));
UnwindOperation unwindOperation = Aggregation.unwind("stats");
GroupOperation groupOperation = Aggregation.group("id", "discordId")
.push("stats").as("stats");
AggregationOperation addFields = (AggregationOperationContext aggregationOperationContext) -> {
DBObject dbObject =
new BasicDBObject("allGamesOnChannel",
new BasicDBObject("$sum", "$stats.pickupsPlayed"));
return new BasicDBObject("$addFields", dbObject);
};
SortOperation sortOperation = Aggregation.sort(new Sort(Sort.Direction.DESC, "allGamesOnChannel"));
LimitOperation limitOperation = Aggregation.limit(maxElements);
ProjectionOperation projectionOperation = Aggregation.project("id", "discordId", "stats");
Aggregation aggregation = Aggregation.newAggregation(matchOperation,unwindOperation, matchOperation,groupOperation,addFields,
sortOperation,limitOperation,projectionOperation);
AggregationResults<UserSummaryChannel> userSummaries = mongoTemplate.aggregate(aggregation, "pickupUser", UserSummaryChannel.class);
Run Code Online (Sandbox Code Playgroud)
我在上面执行时遇到这个异常:
Caused by: java.lang.IllegalArgumentException: Invalid reference 'allGamesOnChannel'!
at org.springframework.data.mongodb.core.aggregation.ExposedFieldsAggregationOperationContext.getReference(ExposedFieldsAggregationOperationContext.java:99) ~[spring-data-mongodb-1.10.10.RELEASE.jar:na]
at org.springframework.data.mongodb.core.aggregation.ExposedFieldsAggregationOperationContext.getReference(ExposedFieldsAggregationOperationContext.java:80) ~[spring-data-mongodb-1.10.10.RELEASE.jar:na]
at org.springframework.data.mongodb.core.aggregation.SortOperation.toDBObject(SortOperation.java:73) …Run Code Online (Sandbox Code Playgroud) 我希望能够以时间解析时间(天,小时,分钟)+可选的字符串,所以输入如下所示:<time>(white_spaces)<optional_string>。我知道正则表达式是处理这些事情的正确工具,所以我想出了这样的表达:
Pattern.compile("((?<days>\\d+)d)?((?<hours>\\d+)h)?((?<minutes>\\d+)m)?\\s+?(?<reason>.+)?");
Run Code Online (Sandbox Code Playgroud)
基本上它按预期工作,但是在此表达式中,所有时间组(天、小时、分钟)都是可选的,我希望输入至少包含分钟组。但是,如果指定了小时或天,则不需要分钟。此外,时间组(d+h、h+m、d+m、d+h+m)的所有组合都是可能的。那么我该如何纠正我的表达呢?或者也许有其他方法来解析一段时间?
编辑:输入示例:
12h64m - 正确的
12d43m dsd - 正确的
- 空字符串 - 不正确
12m - 正确的
12d32h43m - 正确的
sdsds - 不正确 - 没有“指定时间组”