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表明分頁大小;排序