• 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);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    返回json

    {
    	"list": [
    		{
    			"deviceId": "deviceId",
    			"startTime": 1713408948096,
    			"colletion2": {
    				"unitName": "华润北京unitName公司",
    				"unitCode": "unitCode",
    			},
    		}
    	]
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    进一步优化代码

    在聚合查询结果中将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);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    返回json

    {
    	"list": [
    		{
    			"deviceId": "deviceId",
    			"startTime": 1713408948096,
    			"unitName": "华润北京unitName公司"
    		}
    	]
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  • 相关阅读:
    使用 PHP、PDO 和 MySQL 的 CRUD 应用程序
    智源AI日报(2022-09-02):我是如何从头开始写一篇顶级论文的
    ARM裸板调试之串口打印及栈初步分析
    AJAX小结一
    【Python自然语言处理】概率上下文无关文法(PCFG)及神经网络句法分析讲解(图文解释 超详细)
    系统的频域分析法-----Chap4_1-4
    (附源码)计算机毕业设计SSM教学团队管理系统
    在Spring Boot中处理HTTP请求【后端 5】
    政策解读:《科技保险业务统计制度》带来新要求,数据报送攻略大公开!
    Vite 设置 build 之后项目的相对路径,而不是绝对路径 base
  • 原文地址:https://blog.csdn.net/weixin_43833540/article/details/137968250