我正在尝试在程序中将聚合与排序操作结合起来,但似乎不起作用。
GroupOperation groupByDate = Aggregation.group("resetDateTime").push("$$ROOT").as("resetDataList");
MatchOperation untilDate = Aggregation.match(new Criteria("resetDateTime").lte(LocalDateTime.of(startDate.toLocalDate(), LocalTime.MAX)));
MatchOperation relevantId = Aggregation.match(new Criteria("id").is(id));
SortOperation sort = Aggregation.sort(Sort.Direction.DESC, "resetDateTime");
Aggregation aggregation = Aggregation.newAggregation(relevantId , untilDate, sort, groupByDate);
在“ groupByDate” 参数前后放置“ sort” 参数均无效。 输出未排序。 有谁知道为什么它不起作用?
这是不带排序参数的聚合操作的输出(我打印到每个组的“ dateTime” 字段:
[ResetDataGroupByDate(resetDateTime = 2019-10-14T17:37:35.044), ResetDataGroupByDate(resetDateTime = 2019-10-13T17:37:35.015), ResetDataGroupByDate(resetDateTime = 2019-10-12T17:37:35.942), ResetDataGroupByDate(resetDateTime = 2019-10-03T17:37:35.137), ResetDataGroupByDate(resetDateTime = 2019-10-11T17:37:35.885), ResetDataGroupByDate(resetDateTime = 2019-10-07T17:37:35.624), ResetDataGroupByDate(resetDateTime = 2019-10-09T17:37:35.737), ResetDataGroupByDate(resetDateTime = 2019-10-02T17:37:35.096), ResetDataGroupByDate(resetDateTime = 2019-10-01T17:37:35.826), ResetDataGroupByDate(resetDateTime = 2019-10-10T17:37:35.834), ResetDataGroupByDate(resetDateTime = 2019-10-15T17:37:35.106), ResetDataGroupByDate(resetDateTime = 2019-10-05T17:37:35.350), ResetDataGroupByDate(resetDateTime = 2019-10-04T17:37:35.193), ResetDataGroupByDate(resetDateTime = 2019-10-06T17:37:35.397), ResetDataGroupByDate(resetDateTime = 2019-10-08T17:37:35.706)]
答案 0 :(得分:0)
分组后,字段resetDateTime是下一个管道的id字段,因此您的排序应在id字段上,而不是在resetDateTime上。
SortOperation sort = Aggregation.sort(Sort.Direction.DESC, "_id");