首页 > 解决方案 > 如何在spring boot mongoDB中进行聚合和排序?

问题描述

我有以下模型结构:

@Getter
@Setter
@Document(collection = "tbl_user")
public class UsersInfo extends BaseModel {
   public UserContentInfo userContentInfoList;
}

public class UserContentInfo 
{
   private List<VideoInfo> videoPost;
   private List<VideoInfo> highlightVideos;
   private List<ImageInfo> imageInfoList;
}

VideoInfo

public class VideoInfo 
{
  private String videoUrl;
  private Long addedAt;
}

我正在尝试基于sorting.

Aggregation aggregation = Aggregation.newAggregation(Aggregation.match(Criteria.where(Constant.COMMON_ID).is(userId)), Aggregation.sort(Sort.Direction.DESC, "userContentInfoList.videoPost.addedAt").and(Sort.Direction.DESC,
                    "userContentInfoList.highlightVideos.addedAt")
    );

return mongoOperations.aggregate(aggregation, UsersInfo.class, UsersInfo.class);

但这给我带来了以下错误:

Command failed with error 2 (BadValue): 'PlanExecutor error during aggregation :: caused by :: cannot sort with keys that are parallel arrays' on server

我在这里做错了什么?

标签: javamongodbspring-bootmongodb-queryaggregate

解决方案


推荐阅读