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));

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

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐