【发布时间】:2016-01-17 12:31:11
【问题描述】:
我想找到最近的商店。所以我写了这段代码:
public class ShopRepositoryImpl extends GenericRepositoryImpl<ShopEntity, String> implements ShopRepositoryCustom {
@Autowired
protected MongoTemplate template;
@Override
public GeoResult<ShopEntity> findNearest(Point location) {
List<GeoResult<ShopEntity>> result = template.geoNear(
NearQuery.near(location).maxDistance(new Distance(70, Metrics.KILOMETERS))
.query(new Query(Criteria.where("location")).limit(1)),
ShopEntity.class).getContent();
if (result.isEmpty()) {
return null;
} else {
return result.get(0);
}
}
}
在tomcat控制台中有:
2016-01-17 12:13:26.151 WARN 645 --- [nio-8080-exec-2] o.s.data.mongodb.core.MongoTemplate : Command execution of
{ "geoNear" : "shop" , "query" : { "location" : { }} , "maxDistance" : 0.010974991600211786 , "distanceMultiplier" : 6378.137 , "num" : 1 , "near" : [ 48.8703939 , 2.0] , "spherical" : true}
failed: no geo indices for geoNear
我试过这个:
public interface ShopRepository extends GenericRepository<ShopEntity, String>, ShopRepositoryCustom {
GeoResults<ShopEntity> findByLocationNear(Point location, Distance distance);
}
没有结果
在我的数据库中,我有一个元素:
{
"name": "A shop",
"street": "149 Rue Montmartre",
"city": "Paris",
"zip": "75002",
"country": "France",
"location": {
"x": 48.8703937,
"y": 2.3422999
}
}
【问题讨论】:
-
我的实体有:@GeoSpatialIndexed private double[] location;
标签: java mongodb spring-data spring-data-mongodb