投影后按嵌套字段排序的问题

时间:2021-04-13 11:54:41

标签: spring mongodb aggregation

我正在创建聚合,它应该根据条件添加带有值的布尔字段, 然后按此值和另一个值排序。

问题是:使用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");
  }
}

但是,我还是想知道是什么原因导致了这个问题,如果我做错了什么还是某种错误

0 个答案:

没有答案
相关问题