英文:
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="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?
答案1
得分: 0
已修复,使用版本为2.2.6.RELEASE。
英文:
Fixed using version 2.2.6.RELEASE
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论