MongoDB criteria.andOperator报错:you can‘t add a second ‘$or‘ expression specified

在MongoDB中,当需要组合多个查询条件时,可以使用`Criteria`类的`andOperator`方法。然而,在使用过程中可能会遇到错误提示:`you can’t add a second ‘$or’ expression specified`。该问题通常出现在尝试添加第二个`$or`表达式时。要正确地使用`andOperator`,应该确保所有条件在同一层次。以下是一个正确的示例代码:

MongoDB criteria.andOperator报错:you can’t add a second ‘$or’ expression specified

如何组装多个criteria查询条件,以及正确的使用andOperator


    public static void main(String[] args) {
        // 多个条件
        Criteria criteria1 = new Criteria("a1").is("1");
        Criteria criteria2 = new Criteria("a2").ne("2");
        Criteria criteria3 = new Criteria("a3").gt("3");

        // 将多个条件组成一个条件组
        Criteria [] criteriaArr = new Criteria[3];
        criteriaArr[0] = criteria1;
        criteriaArr[1] = criteria2;
        criteriaArr[2] = criteria3;
        // 将一个条件组一次加入调用andOperator
        new Criteria().andOperator(criteriaArr);
    }
@PostMapping("/page") @Operation(summary = "获取台账分页列表") public CommonResult<?> queryPageList(@RequestBody Ledger sup, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) { packageQuery(sup); // 验证传递的值 System.out.println("assetType: " + sup.getAssetType()); System.out.println("getCarModule: " + sup.getCarModule()); System.out.println("contractNum: " + sup.getContractNum()); System.out.println("entityDesc: " + sup.getEntityDesc()); System.out.println("supplierCode: " + sup.getSupplierCode()); System.out.println("supplierName: " + sup.getSupplierName()); System.out.println("status: " + sup.getStatus()); System.out.println("sapCode: " + sup.getSapCode()); System.out.println("stockCode: " + sup.getStockCode()); System.out.println("toolName: " + sup.getToolName()); System.out.println("toolNumber: " + sup.getToolNumber()); Criteria criteria = new Criteria(); if (sup.getAssetType() != null && !sup.getAssetType().isEmpty()) { Pattern pattern = Pattern.compile(".*" + sup.getAssetType() + ".*", Pattern.CASE_INSENSITIVE); criteria.orOperator(Criteria.where("assetType").regex(pattern)); } if (sup.getCarModule() != null && !sup.getCarModule().isEmpty()) { Pattern pattern = Pattern.compile(".*" + sup.getCarModule() + ".*", Pattern.CASE_INSENSITIVE); criteria.orOperator(Criteria.where("carModule").regex(pattern)); } if (sup.getContractNum() != null && !sup.getContractNum().isEmpty()) { Pattern pattern = Pattern.compile(".*" + sup.getContractNum() + ".*", Pattern.CASE_INSENSITIVE); criteria.orOperator(Criteria.where("contractNum").regex(pattern)); } if (sup.getEntityDesc() != null && !sup.getEntityDesc().isEmpty()) { Pattern pattern = Pattern.compile(".*" + sup.getEntityDesc() + ".*", Pattern.CASE_INSENSITIVE); criteria.orOperator(Criteria.where("entityDesc").regex(pattern)); } if (sup.getSupplierCode() != null && !sup.getSupplierCode().isEmpty()) { Pattern pattern = Pattern.compile(".*" + sup.getSupplierCode() + ".*", Pattern.CASE_INSENSITIVE); criteria.orOperator(Criteria.where("supplierCode").regex(pattern)); } if (sup.getSupplierName() != null && !sup.getSupplierName().isEmpty()) { Pattern pattern = Pattern.compile(".*" + sup.getSupplierName() + ".*", Pattern.CASE_INSENSITIVE); criteria.orOperator(Criteria.where("supplierName").regex(pattern)); } if (sup.getStatus() != null && !sup.getStatus().isEmpty()) { criteria.orOperator(Criteria.where("status").is(sup.getStatus())); } if (sup.getSapCode() != null && !sup.getSapCode().isEmpty()) { Pattern pattern = Pattern.compile(".*" + sup.getSapCode() + ".*", Pattern.CASE_INSENSITIVE); criteria.orOperator(Criteria.where("sapCode").regex(pattern)); } if (sup.getStockCode() != null && !sup.getStockCode().isEmpty()) { Pattern pattern = Pattern.compile(".*" + sup.getStockCode() + ".*", Pattern.CASE_INSENSITIVE); criteria.orOperator(Criteria.where("stockCode").regex(pattern)); } if (sup.getToolName() != null && !sup.getToolName().isEmpty()) { Pattern pattern = Pattern.compile(".*" + sup.getToolName() + ".*", Pattern.CASE_INSENSITIVE); criteria.orOperator(Criteria.where("toolName").regex(pattern)); } if (sup.getToolNumber() != null && !sup.getToolNumber().isEmpty()) { Pattern pattern = Pattern.compile(".*" + sup.getToolNumber() + ".*", Pattern.CASE_INSENSITIVE); criteria.orOperator(Criteria.where("toolNumber").regex(pattern)); } Query query = new Query(criteria); // 排序:默认按创建时间倒序 query.with(Sort.by(Sort.Direction.DESC, "createTime")); IPage<Ledger> pageList = ledgerService.page(query, pageNo, pageSize); JSONObject jsonObject = BeanUtils.toBean(pageList, JSONObject.class); JSONObject removeNullValues = MongodbStrUtils.removeNullValues(jsonObject); return success(removeNullValues); }status查询报错 Due to limitations of the org.bson.Document, you can't add a second '$or' expression specified as '$or : [Document{{status=1}}]'; Criteria already contains '$or : [Document{{assetType=.*器具.*}}]'
最新发布
08-23
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值