无法使用@GeoSpatialIndexed 在模型中查询 geoNear

Can't query geoNear in a model with @GeoSpatialIndexed

我有一个这样的模型:

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

并使用 spring-data-mongodb 和 mongo 模板,我正在尝试查询基于点的模型接近地址:

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

但问题是当我执行该方法时,我得到了这个错误:

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

正在搜索此错误,看来我需要 向 mongo 集合添加索引 ,例如:

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

据我了解,这是@GeoSpatialIndexed 注释的地址,但它不起作用

我错过了什么?

已使用 2.2 版修复。6.RELEASE