无法在带有@GeoSpatialIndexed的模型中查询geoNear。

huangapple go评论94阅读模式
英文:

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 与 mongoTemplate,我尝试查询一个基于 Point 的模型,该 Point 靠近 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: 查询失败,错误代码为 291,错误消息为 '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 注解中的 address 控制,但它不起作用

我错过了什么?

英文:

I have a model like that:

@Document(value=&quot;Person&quot;)
class Person {
    @Id
    private int id;
    @GeoSpatialIndexed(name = &quot;address&quot;, 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&lt;Person&gt; findNearestUser(GeoJsonPoint point) {
    Criteria criteria = Criteria
            .where(&quot;address&quot;)
            .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 &#39;db.person.ensureIndex({ &quot;address&quot;: &quot;2dsphere&quot; })&#39;

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

What am I missing?

答案1

得分: 0

已修复,使用版本为2.2.6.RELEASE。

英文:

Fixed using version 2.2.6.RELEASE

huangapple
  • 本文由 发表于 2020年10月26日 23:08:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/64539777.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定