简体   繁体   中英

How to ignore null parameters in mongodb criteria query

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

My code basically works like that. My aim is to create search mechanism where user will enter a location info to start searching and then by adding extra filters customize his/her search. I put them in andOperator because i want this search to be specified to location parameter.

Eg : You search for Germany then you add filter for pricing for example between 0-100 then search should act : return houses from germany that is priced between 0-100

But there is a problem. When an user enters location query model only consist of location parameter which means other parameters in query model is null and when it enters andOperator it gives null pointer exception. I want it to ignore null parameters and move on with the other ones. How can i achieve that?

I tried adding ne(null) field but it didnt help

 Criteria.where("location.coords").ne(null).within(box));

thanks in advance,

Nothing to do with the query. Solution-1 : You just need to tell jackson mapper to exclude all the null values at class level.

@JsonInclude(Include.NON_NULL)
public class MyDto {

private String myfield;

// Getter and Setter   
}

Solution-2 : You just simple add following property in your .yml or .properties file.

jackson:
  default-property-inclusion: non-null

Surprisingly, second method worked for me wonder .....!

You need to improve your query like this:

db.getCollection('criteria').find({
 'location.coords': {
     $ne: null
 }
});

Just Change your query with below and try this one.

Query query = new Query();
Criteria criteria = new Criteria();
List<Criteria> orCriterias = new ArrayList<>();
if( published != null) {    
  orCriterias.add(Criteria.where("published").is(published)));
}
... so on for other fields
criteria.orOperator(orCriterias.toArray(new Criteria[orCriterias.size()]));
query.addCriteria(criteria);
List<Property> recordsList = mongoTemplate.find(query, Property.class, 

"collecion name");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM