多层级查询
注意:要注意代码顺序 查询层级数据代码放前面,查询条件放后面
if (StringUtils.isBlank(params.getDocType())) {params.setDocType(DOC_TDCTYPE);}String docName = mapper.findByDocInfo(params.getDocType());List<ExpertApprovalOpinionsVO> expertApprovalOpinionsVOS = new ArrayList<>();String opinion = params.getApprovalOpinions();String chapterName = params.getChapterName();String approvalPerson = params.getApprovalPerson();Criteria criteria = new Criteria();Criteria[] criteriaArray = new Criteria[0];List<AggregationOperation> operations = new ArrayList<>();//分页条件int skip = (page - 1) * pageSize;AggregationOperation skipOperation = Aggregation.skip((long) skip);AggregationOperation limitOperation = Aggregation.limit((long) pageSize);//多层级查询条件// 展开第一层级数组commentThreadsAggregationOperation unwind1 = Aggregation.unwind(COMMENTTHREADS);// 展开第二层级数组commentThreads.commentsAggregationOperation unwind2 = Aggregation.unwind(COMMENTTHREADS_COMMENTS);operations.add(unwind1);operations.add(unwind2);//添加条件查询 ,单个查询条件和 多个 查询值if (StringUtils.isNotBlank(chapterName)) {//查詢出 章节key 用于根据章节key查询 MongoDB 的章节批注意见List<Map<String, Object>> modelKeyList = mapper.findByModelKey(params);if (!modelKeyList.isEmpty()) {// 匹配满足单次正则表达式的文档if (modelKeyList.size() == 1) {Pattern pattern = Pattern.compile(ObjectUtils.toString(modelKeyList.get(0).get(MODEL_KEY)), Pattern.CASE_INSENSITIVE); // 根据您的需要指定模糊匹配的正则表达式和匹配选项MatchOperation match = Aggregation.match(Criteria.where(KEY).regex(pattern));operations.add(match);} else {// 匹配满足批量正则表达式的文档// 章节key集合List<String> threadId = modelKeyList.stream().map(map -> map.get(MODEL_KEY) != null ? String.valueOf(map.get(MODEL_KEY)) : null).filter(Objects::nonNull).collect(Collectors.toList());// 构建匹配条件列表List<Criteria> criteriaList = new ArrayList<>();for (String pattern : threadId) {// 构造正则表达式匹配条件String regexPattern = ".*" + Pattern.compile(pattern) + ".*";criteriaList.add(Criteria.where(KEY).regex(regexPattern));}// 使用$or操作符组合多个条件Criteria orCriteria = new Criteria().orOperator(criteriaList.toArray(new Criteria[criteriaList.size()]));AggregationOperation match2 = Aggregation.match(orCriteria);operations.add(match2);}} else {return expertApprovalOpinionsVOS;}}if (StringUtils.isNotBlank(approvalPerson)) {//模糊查询出用户List<Map<String, Object>> userList = mapper.findByUser(params);if (!userList.isEmpty()) {//用户集合List<String> authorId = userList.stream().map(map -> map.get(USER_ID) != null ? String.valueOf(map.get(USER_ID)) : null).filter(Objects::nonNull).collect(Collectors.toList());criteria = Criteria.where(COMMENTTHREADS_COMMENTS_AUTHORID).in(authorId);AggregationOperation match = Aggregation.match(new Criteria().andOperator(criteria));operations.add(match);} else {return expertApprovalOpinionsVOS;}}if (StringUtils.isNotBlank(opinion)) {Pattern pattern1 = Pattern.compile(opinion, Pattern.CASE_INSENSITIVE);criteria = Criteria.where(COMMENTTHREADS_COMMENTS_CONTENT).regex(pattern1);AggregationOperation match2 = Aggregation.match(criteria);operations.add(match2);}/*** project 新的 数据 包含 "crt_time", "nodeId", "processInstanceId", "nodeCrtTime"* andExclude 排除 "_id"* andExpression 执行 表达式 ,取出对应的 数据值* as 生成的 新数据的 名称*/operations.add(Aggregation.project("crt_time", "nodeId", "processInstanceId", "nodeCrtTime").andExclude("_id").andExpression(COMMENTTHREADS_COMMENTS).as(COMMENTS));operations.add(Aggregation.sort(Sort.Direction.DESC, "crt_time"));operations.add(skipOperation);operations.add(limitOperation);Aggregation aggregation = Aggregation.newAggregation(operations);//查询AggregationResults<Map> result = template.aggregate(aggregation, params.getDocType() + ".approvals", Map.class);//获取结果List<Map> mappedResults = result.getMappedResults();