Mongodb聚合查询:经过文章的阅读量,评论量,点赞量等计算出一个热度值用于排序

spring data es实现方式:spring

TypedAggregation<Album> agg = Aggregation.newAggregation(
        Album.class,
        Aggregation.project("id","albumName","coverImgId","popular").and("countRead").plus("countComment").plus("countLike").as("polular"),
        Aggregation.sort(Sort.Direction.DESC,"popular"),
        Aggregation.skip (page),
        Aggregation.limit(pageSize));
AggregationResults<PoplularAlbumDto> results=mongoTemplate.aggregate(agg, PoplularAlbumDto.class);

解析:.project()里可指定要查询的字段,还可使用一些字段的加减乘除运算出一个新的字段,上面的是“countRead”,"countComment","countLIke"这几个字段的和做为一个新的字段“polular”,并按popular降序排序,skip表明页码,limit表明分页大小;排序

相关文章
相关标签/搜索