栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【MongoDB】SpringData -在MongoRepository中使用mongoTemplate

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【MongoDB】SpringData -在MongoRepository中使用mongoTemplate

一、创建MyRepository接口

在MyRepository中引用MongoTemplate

public interface MyRepository {
    Page findTest(SUser sUser, Pageable pageable);
}
二、创建实现类MyRepositoryImpl实现MyRepository

包含Aggregate用法

public class MyRepositoryImpl implements MyRepository {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Page findTest(SUser sUser, Pageable pageable) {
        ObjectId objectId = new ObjectId(sUser.getId());

        Aggregation aggregation = Aggregation.newAggregation(
            aoc ->
                new Document(
                    "$addFields",
                    new Document("user", new Document("$arrayElemAt", Arrays.asList(new Document("$objectToArray", "$user"), 1L)))
                ),
            aoc -> new Document("$addFields", new Document("user", "$user.v")),
            Aggregation.match(Criteria.where("user").is(objectId)),
            aoc ->
                new Document(
                    "$addFields",
                    new Document("reading", new Document("$arrayElemAt", Arrays.asList(new Document("$objectToArray", "$reading"), 1L)))
                ),
            aoc -> new Document("$addFields", new Document("reading", "$reading.v")),
            Aggregation.lookup("reading", "reading", "_id", "reading"),
            aoc -> new Document("$replaceRoot", new Document("newRoot", new Document("$first", "$reading"))),
            Aggregation.project(
                "id",
                "title",
                "author",
                "genre",
                "alignment",
                "text_summary",
                "clicks",
                "language",
                "is_private",
                "length",
                "date_created",
                "last_updated",
                "uploader",
                "tags",
                "playlists"
            ),
            Aggregation.sort(pageable.getSort()),
            Aggregation.skip(pageable.getPageNumber() * pageable.getPageSize()),
            Aggregation.limit(pageable.getPageSize())
        );

        Aggregation aggregationCount = Aggregation.newAggregation(
            aoc ->
                new Document(
                    "$addFields",
                    new Document("user", new Document("$arrayElemAt", Arrays.asList(new Document("$objectToArray", "$user"), 1L)))
                ),
            aoc -> new Document("$addFields", new Document("user", "$user.v")),
            Aggregation.match(Criteria.where("user").is(objectId)),
            Aggregation.group().count().as("count")
        );

        AggregationResults readings = mongoTemplate.aggregate(aggregation, "favorite", Reading.class);
        int count = mongoTemplate.aggregate(aggregationCount, "favorite", ResultCount.class).getMappedResults().get(0).count;

        List mappedResults = readings.getMappedResults();
        Page page = new PageImpl<>(mappedResults, pageable, count);

        return page;
    }

    class ResultCount {

        int count;
    }
}
三、在Repository继承MyRepositoryImpl实现代码即可
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/826686.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号