Using Spring-Data-Mongo how do I limit search?
I am trying to find everyone over 19 but under 50 years old? the field is called "age". The following code is NOT working
public void countUnderAge()
{
List<Person> results = null;
Query query = new Query();
Criteria criteria = new Criteria();
criteria = criteria.lte("21");
query.addCriteria(criteria);
results = mongoTemplate.find(query, Person.class);
logger.info("Total number of under age in database: {}", results.size());
}
Use this:
Query query= new Query();
query.addCriteria(where("age").lte(21));
List<Person> results = mongoTemplate.find(query, Person.class);
You are missing the field name of the where clause. You can also do:
Query query = new Query();
Criteria criteria = new Criteria();
criteria = where("age").lte("21");
query.addCriteria(criteria);
To use "where" you need to add this import:
import static org.springframework.data.mongodb.core.query.Criteria.where;
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.