我有一个搜索表单用于在我的 Web 应用程序中搜索房屋。表单具有以下字段,
private String type;
private Long minPrice;
private Long maxPrice;
private Integer minBedRooms;
此表格用于在所有字段中输入数据时搜索房屋。例如,用户"Rent 200 5000 2"
分别在所有字段中输入数据。我的查询是
Query query = new Query(where("type")
.is(form.getType())
.and("bedrooms")
.is(form.getMinBedRooms())
.andOperator(where("price").lte(form.getMaxPrice()),
where("price").gte(form.getMinPrice())));
现在,当我将上述字段之一留空时,它会返回0
结果。例如当maxPrice
为空时,它返回0
结果。我的问题是我应该如何编写上述查询,以便它可以忽略空值。例如,我将maxPrice
andminBedRooms
字段留空,它将根据type
and搜索数据minPrice
我正在使用 spring 4. 和 mongoDB 的 spring 数据
我已经坚持了两天了,我将非常感谢任何帮助。问候,