最近项目用到了MongoDB,在使用聚合的时候发现排序没有生效.  后来发现是sort位置原因.

public List<TrackTrace> findPoint(List<String> jobNos) {

        Sort sort = new Sort(Sort.Direction.DESC, "traceDate");
        Aggregation agg =
                Aggregation.newAggregation(
                        Aggregation.project("jobNo", "points", "traceDate", "time"),
                        Aggregation.match(Criteria.where("jobNo").in(jobNos)),
                        Aggregation.sort(sort),//在聚合之前对数据进行排序
                        Aggregation.group("jobNo")
                                .first("jobNo").as("jobNo")
                                .first("points").as("points")
                                .first("traceDate").as("traceDate")
                );
        AggregationResults<TrackTrace> durationData =
                mongoTemplate.aggregate(agg, "TrackTrace", TrackTrace.class);
        List<TrackTrace> traces = durationData.getMappedResults();
        return traces;
    }

 

相关文章:

  • 2022-12-23
  • 2021-06-09
  • 2021-07-12
  • 2021-05-25
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-15
  • 2021-10-11
  • 2022-12-23
  • 2021-12-03
  • 2022-12-23
  • 2021-04-03
  • 2021-08-15
相关资源
相似解决方案