mongodb 实现两个集合的关联并分页查询

问题描述

实现两个集合的关联并分页查询。
假设:
collection1中有deviceId等字段,collection2 中有deviceId、unitName等字段, 关联这两个colltion,并分页查询

代码实现

    public ResponseEntity<String> getPageList(@RequestBody DevQO qo) {
        Aggregation aggregation;
        int pageNo = qo.getPageNo(), pageSize = 2;
        List<AggregationOperation> operations = new ArrayList<>();
        if (qo.getDeviceId() != null) {
            operations.add(Aggregation.match(Criteria.where("deviceId").is(qo.getDeviceId())));
        }
        operations.add(Aggregation.lookup("collection2", "deviceId", "deviceId", "collection2"));
        if (qo.getUnitName() != null) {
            operations.add(Aggregation.match(Criteria.where("collection2.unitName").is(qo.getUnitName())));
        }
        operations.add(Aggregation.sort(Sort.Direction.DESC, "startTime"));
        operations.add(Aggregation.skip((long) (pageNo - 1) * pageSize));// 跳过之前页面的文档
        operations.add(Aggregation.limit(pageSize));// 跳过之前页面的文档
        aggregation = Aggregation.newAggregation(operations);
        AggregationResults<DeviceStatusInfo> pageData =
                mongoTemplate.aggregate(aggregation, "alarm_data", DeviceStatusInfo.class);
        List<DeviceInfo> list = pageData.getMappedResults();
        JSONObject res = new JSONObject();
        resJson.put("list", list);
        return new ResponseEntity<>(JSON.toJSONString(res, SerializerFeature.WriteDateUseDateFormat), HttpStatus.OK);
    }

返回json

{
	"list": [
		{
			"deviceId": "deviceId",
			"startTime": 1713408948096,
			"colletion2": {
				"unitName": "华润北京unitName公司",
				"unitCode": "unitCode",
			},
		}
	]
}

进一步优化代码

在聚合查询结果中将collection2中的unitName,移动到与“startTime”同级的位置,并删除“collection2”字段

    public ResponseEntity<String> getPageList(@RequestBody DevQO qo) {
        Aggregation aggregation;
        int pageNo = qo.getPageNo(), pageSize = 2;
        List<AggregationOperation> operations = new ArrayList<>();
        if (qo.getDeviceId() != null) {
            operations.add(Aggregation.match(Criteria.where("deviceId").is(qo.getDeviceId())));
        }
        operations.add(Aggregation.lookup("collection2", "deviceId", "deviceId", "collection2"));
        if (qo.getUnitName() != null) {
            operations.add(Aggregation.match(Criteria.where("collection2.unitName").is(qo.getUnitName())));
        }
            operations.add(Aggregation.project()
                .andInclude("deviceId", "startTime")
                .and("collection2.unitName").as("unitName"));
        operations.add(Aggregation.sort(Sort.Direction.DESC, "startTime"));
        operations.add(Aggregation.skip((long) (pageNo - 1) * pageSize));// 跳过之前页面的文档
        operations.add(Aggregation.limit(pageSize));// 跳过之前页面的文档
        aggregation = Aggregation.newAggregation(operations);
        AggregationResults<DeviceStatusInfo> pageData =
                mongoTemplate.aggregate(aggregation, "alarm_data", DeviceStatusInfo.class);
        List<DeviceInfo> list = pageData.getMappedResults();
        JSONObject res = new JSONObject();
        resJson.put("list", list);
        return new ResponseEntity<>(JSON.toJSONString(res, SerializerFeature.WriteDateUseDateFormat), HttpStatus.OK);
    }

返回json

{
	"list": [
		{
			"deviceId": "deviceId",
			"startTime": 1713408948096,
			"unitName": "华润北京unitName公司"
		}
	]
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值