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

落花浮王杯 提交于 2019-11-29 19:08:44

spring data es实现方式:

TypedAggregation<Album> agg = Aggregation.newAggregation(
        Album.class,
        Aggregation.project("id","albumName","coverImgId").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代表分页大小;

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!