wol*_*olf 5 java mongodb mongodb-query spring-boot
List<Property> properties;
Criteria criteria = new Criteria();
criteria.andOperator(
Criteria.where("published").is(true),
Criteria.where("location.district").regex(model.getLocation(),"i").orOperator(Criteria.where("location.city").exists(true).regex(model.getLocation(),"i")),
Criteria.where("basicInfo.propertyType").is(model.getPropertyType()),
Criteria.where("priceInfo.price").gte(prices[0]).lte(prices[1]),
Criteria.where("bedspace").gte(model.getAdult()).orOperator(Criteria.where("bedspace").gte(model.getChild())),
Criteria.where("unavailableDates").ne(model.getCheckinDate()).andOperator(Criteria.where("unavalibleDates").ne(model.getCheckoutDate())),
Criteria.where("location.coords").within(box));
Query query = new Query(criteria);
properties = mongoTemplate.find(query, Property.class);
Run Code Online (Sandbox Code Playgroud)
我的代码基本上是这样工作的。我的目标是创建搜索机制,用户将输入位置信息来开始搜索,然后通过添加额外的过滤器自定义他/她的搜索。我将它们放入 andOperator 中,因为我希望将此搜索指定为位置参数。
例如:您搜索德国,然后添加价格过滤器,例如 0-100 之间,然后搜索应该执行:返回来自德国的价格在 0-100 之间的房屋
但有一个问题。当用户进入位置查询模型时,仅包含位置参数,这意味着查询模型中的其他参数为空,当用户进入andOperator时,会出现空指针异常。我希望它忽略空参数并继续处理其他参数。我怎样才能做到这一点?
我尝试添加 ne(null) 字段,但没有帮助
Criteria.where("location.coords").ne(null).within(box));
Run Code Online (Sandbox Code Playgroud)
提前致谢,
您需要像这样改进您的查询:
db.getCollection('criteria').find({
'location.coords': {
$ne: null
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6309 次 |
| 最近记录: |