【问题标题】:Can't query geoNear in a model with @GeoSpatialIndexed无法使用 @GeoSpatialIndexed 在模型中查询 geoNear
【发布时间】:2021-02-08 20:47:51
【问题描述】:

我有一个这样的模型:

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

并且使用带有 mongoTemplate 的 spring-data-mongodb,我正在尝试查询基于 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));
}

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

无法在服务器 localhost:27017 上找到 $geoNear 查询的索引; 嵌套异常是 com.mongodb.MongoQueryException: Query failed with 错误代码 291 和错误消息“错误处理查询: ns=local.Person limit=1Tree: GEONEAR 字段=地址 maxdist=1.79769e+308 isNearSphere=0

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

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

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

我错过了什么?

【问题讨论】:

    标签: java spring mongodb geojson spring-data-mongodb


    【解决方案1】:

    已使用 2.2.6.RELEASE 版本修复

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-05
      • 1970-01-01
      • 2015-11-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-06
      相关资源
      最近更新 更多