Search code examples
springmongodbspring-dataspring-data-mongodbspring-mongo

spring data mongodb NearQuery add another criteria


Is it possible to add a Criteria to a NearQuery?

Currently I am doing this:

public List<UserLocation> getUserNearLocation(Point p, double min, double max){
    NearQuery query = NearQuery.near(p).minDistance(new Distance(min, Metrics.KILOMETERS)).maxDistance(new Distance(max, Metrics.KILOMETERS));
    GeoResults<UserLocation> results = mongoTemplate.geoNear(query, UserLocation.class);

    List<UserLocation> all = new ArrayList<UserLocation>();
    Iterator<GeoResult<UserLocation>> iter = results.iterator();
    while (iter.hasNext()){
        GeoResult<UserLocation> temp = iter.next();
        all.add(temp.getContent());
    }

    return all;     
}

I would like to add another criteria to the query, is it possible and how?


Solution

  • just add an additional query like shown below.

    NearQuery query = NearQuery.near(new Point(1, 2)).query(Query.query(Criteria.where("foo").is("bar")));