解决MongoDB问题:Due to limitations of the com.mongodb.BasicDocument, you cant add a second null criter

1、问题描述

Due to limitations of the com.mongodb.BasicDocument, you can t add a second null criteria. Query already contains { $and : [{ width : { $gte : -10.0}}, { width: { $lte : -80.0}

2、问题产生原因

我在使用mongodb的条件筛选的时候,报了这个错误。我的项目是基于SpringBoot + MongoDB,主要使用的还是MongoDBTemplate,因为感觉更方便一些。在此项目中,我想实现的效果是mongoDB根据多个范围查询来返回筛选后的数据然后通过DataTable插件在前端展示出来。
这时候就会有个问题,我使用了多个.andOperator,然后就报了这个错误。
具体代码

// gte  大于等于   lte 小于等于
query.addCriteria(new Criteria().andOperator(Criteria.where("width").gte(width_start),
                    Criteria.where("width").lte(width_end)));
query.addCriteria(new Criteria().andOperator(Criteria.where("depth").gte(depth_start),
                    Criteria.where("depth").lte(depth_end)));
query.addCriteria(new Criteria().andOperator(Criteria.where("food").gte(food_start),
                    Criteria.where("food").lte(food_end)));               

报错

3、自己的解决方法

c1 = Criteria.where("width").gte(width_start).lte(width_end);
c2 = Criteria.where("depth").gte(depth_start).lte(depth_end);
c3 = Criteria.where("food").gte(food_start).lte(food_end);
query.addCriteria(new Criteria().andOperator(c1, c2, c3));

这样就不会报错了,希望能给大家带来帮助

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

熊凯瑞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值