投影后按嵌套域排序的spring问题

tcbh2hod  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(149)

我正在创建聚合,它应该根据条件添加值为的布尔字段,然后按此值和另一个值排序。
问题是:使用后 ProjectionOperation ,的 SortingOperation 由任何一个类字段抛出 InvalidReference :

{
  "timestamp": "2021-04-13T11:34:14.200+00:00",
  "status": 500,
  "error": "Internal Server Error",
  "message": "Invalid reference 'displayOnTop'!",
}

我就是这样创建聚合的:

TypedAggregation<WorklistStudyEntity> agg =
        Aggregation.newAggregation(
            WorklistStudyEntity.class,
            match,
            ProjectAggregationHelper.get(),
            SortingOperationHelper.get(),
            PaginationAggregationHelper.getSkip(pageable.getPageNumber(), pageable.getPageSize()),
            PaginationAggregationHelper.getLimit(pageable.getPageSize()));

    AggregationResults<WorklistStudyEntity> res =
        mongoTemplate.aggregate(agg, "worklistStudy", WorklistStudyEntity.class);

projectaggregationhelper(简化,仍然会导致相同的问题)

public class ProjectAggregationHelper {

  public static AggregationOperation get() {
    return Aggregation.project(WorklistStudyEntity.class);
  }
}

sortingaggregationhelper(简化):

public class SortingOperationHelper {
  public static AggregationOperation get() {
    return Aggregation.sort(Direction.DESC, "displayOnTop");
  }
}

结果,我想要的是:

[{$project: {
  details: 1,
  timeLogs: 1,
  (and all other fields)
  isSTAT: {
    $cond: {
      if: {
        $eq: ["details.priority", "STAT"]
      }, then: true, else: false
    }
  }
}}, {$sort: {
  "timeLogs.receivedDateTime": 1
}}]

更新

我提供了所有的领域 AggregationOperation.project(String... fields) ```
public class ProjectAggregationHelper {

public static ProjectionOperation get() {
return TypedAggregation.project(
"id",
...
"displayOnTop",
"timeLogs",
"details")
.and("isSTAT")
.applyCondition(Cond.when(isStat()).then(true).otherwise(false));
}

private static Criteria isStat() {
return Criteria.where("details.priority").is("STAT");
}
}

不过,我还是想知道是什么原因造成了这个麻烦,如果我做错了什么或者是某种错误

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题