Search code examples
javaspringmongodbgeojsonspring-data-mongodb

Can't query geoNear in a model with @GeoSpatialIndexed


I have a model like that:

@Document(value="Person")
class Person {
    @Id
    private int id;
    @GeoSpatialIndexed(name = "address", type = GeoSpatialIndexType.GEO_2DSPHERE)
    private GeoJsonPoint address;
}

And using spring-data-mongodb with mongoTemplate, I'm trying to query a model that based in a Point is near to address:

public Optional<Person> findNearestUser(GeoJsonPoint point) {
    Criteria criteria = Criteria
            .where("address")
            .near(point);

    Query query = new Query(criteria);

    return Optional.ofNullable(mongoTemplate.findOne(query, Person.class));
}

But the problem is that when I execute the method, I got this error:

unable to find index for $geoNear query' on server localhost:27017; nested exception is com.mongodb.MongoQueryException: Query failed with error code 291 and error message 'error processing query: ns=local.Person limit=1Tree: GEONEAR field=address maxdist=1.79769e+308 isNearSphere=0

Searching for this error, looks like I need to add an index to the mongo collection like:

mongo --host mongo db --eval 'db.person.ensureIndex({ "address": "2dsphere" })'

And as I understood, this is address by @GeoSpatialIndexed annotation, but it's not working.

What am I missing?


Solution

  • Fixed using version 2.2.6.RELEASE