mongo嵌套内部数据分页查询

document对象

{
    "region": "China",
    "channel": "pc",
    "day": "20240315",
    "objects": [
        {
            "id": "qwer",
            "num": 4456,
            "level": "S"
        },
        {
            "id": "ewrwe",
            "num": 576,
            "level": "S"
        },
        {
            "id": "gegw",
            "num": 568,
            "level": "S"
        },
        {
            "id": "ewhh",
            "num": 132,
            "level": "S"
        },
        {
            "id": "hhwhw",
            "num": 132,
            "level": "S"
        }
    ],
    "create_time": "2024-03-15T15:32:35.715Z"
}

先根据region和channel筛选后获取时间最近的一条数据,对数据的objects集合进行分页查询

AggregationOperation match = Aggregation.match(
    Criteria.where("region").is("China").and("channel").is("pc")
);
AggregationOperation project = Aggregation.project("_id","create_time");
AggregationOperation sort = Aggregation.sort(Sort.by(Sort.Direction.DESC, "create_time"));
AggregationOperation limitToFirst = Aggregation.limit(1);

// 执行聚合查询,获取最新的一条记录
Aggregation aggregationFirst = Aggregation.newAggregation(match, project, sort, limitToFirst);
AggregationResults<MyDoc> aggregationResultsFirst = mongoTemplate.aggregate(aggregationFirst, "my_collection", MyDoc.class);
MyDoc firstRecord = aggregationResultsFirst.getUniqueMappedResult();

// 获取最新记录的 _id
String latestId = firstRecord.getId();

// 构建第二次聚合查询操作,进行分页查询
AggregationOperation matchById = Aggregation.match(Criteria.where("_id").is(latestId));
AggregationOperation unwind = Aggregation.unwind("objects");
AggregationOperation skipOperation = Aggregation.skip((long) (pageSize * (page - 1)));
AggregationOperation limitOperation = Aggregation.limit(pageSize);
AggregationOperation groupOperation = Aggregation.group().push("objects").as("objects").count().as("total");

// 执行第二次聚合查询
Aggregation aggregationSecond = Aggregation.newAggregation(matchById, unwind, skipOperation, limitOperation, groupOperation);
AggregationResults<Document> aggregationResultsSecond = mongoTemplate.aggregate(aggregationSecond, "my_collection", Document.class);

// 获取查询结果
Document resultDocument = aggregationResultsSecond.getUniqueMappedResult();
List<SubDocModel> objects = (List<SubDocModel>) resultDocument.get("objects");
int totalObjectsCount = resultDocument.getInteger("total");

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MongoRepository 是 Spring Data MongoDB 提供的一种 Repository 接口,用于简化 MongoDB 数据库的操作。 要实现分页查询,可以使用 MongoRepository 接口提供的一些分页查询方法,例如: ```java public interface UserRepository extends MongoRepository<User, String> { Page<User> findAll(Pageable pageable); Page<User> findByAgeGreaterThan(int age, Pageable pageable); // 其他自定义查询方法 } ``` 其中,`Pageable` 是 Spring Data 提供的一个分页请求参数接口,包含了分页查询的页码、每页数据量、排序方式等信息。 使用时,可以将 `Pageable` 对象作为参数传入分页查询方法中: ```java Pageable pageable = PageRequest.of(pageNumber, pageSize, Sort.by("age").descending()); Page<User> users = userRepository.findByAgeGreaterThan(18, pageable); ``` 上述代码中,`PageRequest.of()` 方法用于创建 `Pageable` 对象,包含了当前页码、每页数据量和按年龄倒序排序的排序方式,然后调用 `findByAgeGreaterThan()` 方法进行分页查询。 查询结果为 `Page<User>` 类型,包含了当前页的数据和总页数等信息。可以通过 `Page` 对象的方法获取分页信息: ```java users.getContent(); // 当前页的数据列表 users.getTotalElements(); // 总数据量 users.getTotalPages(); // 总页数 users.getNumber(); // 当前页码 users.getSize(); // 每页数据量 users.hasNext(); // 是否有下一页 users.hasPrevious(); // 是否有上一页 ``` 以上就是使用 MongoRepository 实现分页查询的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值