在JAVA中使用mongoTemplate构造查询条件
// 创建条件对象 Criteria criteria = new Criteria(); // 3. 单个条件查询多个字段 (客户编号) if (StringUtils.isNotEmpty(bo.getAdmpId())) { criteria.orOperator( Criteria.where("final_uid").is(bo.getAdmpId()), Criteria.where("customer_ids").in(bo.getAdmpId()), Criteria.where("official_ids").in(bo.getAdmpId()), Criteria.where("tb_ids").in(bo.getAdmpId()), Criteria.where("jd_ids").in(bo.getAdmpId()), Criteria.where("yz_ids").in(bo.getAdmpId()), Criteria.where("wm_ids").in(bo.getAdmpId()), Criteria.where("dd_ids").in(bo.getAdmpId()), Criteria.where("ks_ids").in(bo.getAdmpId()) ); } // 2. 模糊查询 (客户名称模糊搜索) if (StringUtils.isNotBlank(bo.getName())) { criteria.and("name").regex(Pattern.compile("^.*" + bo.getName() + ".*$", Pattern.CASE_INSENSITIVE)); } // 1. 全等于 (手机号全字匹配) if (StringUtils.isNotBlank(bo.getMobile())) { criteria.and("mobile").is(bo.getMobile()); } if (StringUtils.isNotBlank(bo.getBindStatus())) { criteria.and("bind_status").is(bo.getBindStatus()); } if (StringUtils.isNotBlank(bo.getRetentionStatus())) { criteria.and("retention_status").is(bo.getRetentionStatus()); } // 4. 日期范围 (近期消费时间) if (StringUtils.isNotEmpty(bo.getRecentlyBuyBeginTime()) && StringUtils.isNotEmpty(bo.getRecentlyBuyEndTime())) { criteria.andOperator(Criteria.where("recently_buy_time").gte(bo.getRecentlyBuyBeginTime()), Criteria.where("recently_buy_time").lte(bo.getRecentlyBuyEndTime())); } if (StringUtils.isNotNull(bo.getLowestTotalBuyAmount()) && StringUtils.isNotNull(bo.getHighestTotalBuyAmount())) { criteria.and("total_buy_amount").gte(bo.getLowestTotalBuyAmount()).lte(bo.getHighestTotalBuyAmount()); } if (StringUtils.isNotNull(bo.getLowestTotalBuyAmount()) && StringUtils.isNull(bo.getHighestTotalBuyAmount())) { criteria.and("total_buy_amount").gte(bo.getLowestTotalBuyAmount()); } if (StringUtils.isNull(bo.getLowestTotalBuyAmount()) && StringUtils.isNotNull(bo.getHighestTotalBuyAmount())) { criteria.and("total_buy_amount").lte(bo.getHighestTotalBuyAmount()); } // 5. 数值范围 (消费总金额) if (StringUtils.isNotNull(bo.getMinTotalBuyTimes()) && StringUtils.isNotNull(bo.getMaxTotalBuyTimes())) { criteria.and("total_buy_count").gte(bo.getMinTotalBuyTimes()).lte(bo.getMaxTotalBuyTimes()); } if (StringUtils.isNotNull(bo.getMinTotalBuyTimes()) && StringUtils.isNull(bo.getMaxTotalBuyTimes())) { criteria.and("total_buy_count").gte(bo.getMinTotalBuyTimes()); } if (StringUtils.isNull(bo.getMinTotalBuyTimes()) && StringUtils.isNotNull(bo.getMaxTotalBuyTimes())) { criteria.and("total_buy_count").lte(bo.getMaxTotalBuyTimes()); } if (!CollectionUtils.isEmpty(Arrays.asList(bo.getAdmpLabels()))) { if ("all".equals(bo.getTagScope())) { // 7. 数组字段满足全部 (客户标签) criteria.and("admp_labels").all(bo.getAdmpLabels()); } else if ("any".equals(bo.getTagScope())) { criteria.and("admp_labels").in(bo.getAdmpLabels()); } } if (StringUtils.isNotEmpty(bo.getReceiverMobile())) { criteria.and("receiver_mobiles").in(bo.getReceiverMobile()); } // 6. 数组字段满足任一 (来源平台、下单店铺) if (StringUtils.isNotNull(bo.getPlatformTypes()) && bo.getPlatformTypes().length > 0) { criteria.and("source_codes").in(bo.getPlatformTypes()); } if (StringUtils.isNotNull(bo.getShopIds()) && bo.getShopIds().length > 0) { criteria.and("shop_ids").in(bo.getShopIds()); } if (StringUtils.isNotNull(bo.getReceiverProvince()) && bo.getReceiverProvince().length > 0) { criteria.and("receiver_provinces").in(bo.getReceiverProvince()); } if (StringUtils.isNotNull(bo.getReceiverCity()) && bo.getReceiverCity().length > 0) { criteria.and("receiver_cities").in(bo.getReceiverCity()); } if (StringUtils.isNotNull(bo.getReceiverDistrict()) && bo.getReceiverDistrict().length > 0) { criteria.and("receiver_districts").in(bo.getReceiverDistrict()); } Query query = new Query(); query.addCriteria(criteria); // 12. 总记录数 long total = mongoTemplate.count(query, ClientBasicInfoDO.class); // 8. 查询返回指定字段 (自定义列表) query.fields().include("final_uid", "name", "wechat_id", "mobile", "u_id", "retention_status", "tb_ids", "jd_ids", "yz_ids", "tb_ids", "wm_ids", "dd_ids", "ks_ids"); // 10. 分页 query.with(PageRequest.of(bo.getPageNum() - 1, bo.getPageSize(), // 11. 排序 Sort.by(Sort.Order.desc("earliest_add_time")))); // 执行查询 List<ClientBasicInfoDO> list = mongoTemplate.find(query, ClientBasicInfoDO.class);
本文来自博客园,作者:迷糊桃,转载请注明原文链接:https://www.cnblogs.com/mihutao/p/17522327.html