I\'m generating a complicated Mongo
query depending on multiple parameters. One of criterion that I want to make with Criteria
helper class is:
Query query = new Query(Criteria.where("field1").exists(true).ne(false));
Or, if field1 is always a boolean value when present:
Query query = new Query(Criteria.where("field1").is(true));
Since you can’t use Criteria.and() to add multiple criteria into the same field, use Criteria.andOperator() as follows:
Query query = new Query();
query.addCriteria(
new Criteria().andOperator(
Criteria.where("field1").exists(true),
Criteria.where("field1").ne(false)
)
);
List<Foo> result = mongoTemplate.find(query, Foo.class);
System.out.println("query - " + query.toString());
for (Foo foo : result) {
System.out.println("result - " + foo);
}