以下是“Location”集合中的mongo db文档
{ _id: "FortWorth", _class: "com.hack.whereru.Location", position: [ 32.862029, -97.32298 ] }
{ _id: "Richardson", _class: "com.hack.whereru.Location", position: [ 32.930942, -96.626729 ] }
{ _id: "Austin", _class: "com.hack.whereru.Location", position: [ 30.307168, -97.747908 ] }
{ _id: "A", _class: "com.hack.whereru.Location", position: [ 0.001, -0.002 ] }
{ _id: "B", _class: "com.hack.whereru.Location", position: [ 1, 1 ] }
{ _id: "C", _class: "com.hack.whereru.Location", position: [ 0.5, 0.5 ] }
{ _id: "D", _class: "com.hack.whereru.Location", position: [ -0.5, -0.5 ] }
{ _id: "Berlin", _class: "com.hack.whereru.Location", position: [ 13.405838, 52.531261 ] }
{ _id: "Cologne", _class: "com.hack.whereru.Location", position: [ 6.921272, 50.960157 ] }
{ _id: "Dsseldorf", _class: "com.hack.whereru.Location", position: [ 6.810036, 51.224088 ] }
我的代码:
private static final Point DUS = new Point( 13.405838, 52.531261 );
@RequestMapping(value = "/showFromMongos", method = RequestMethod.GET)
public String showFromMogoDB() {
template.indexOps(Location.class).ensureIndex( new GeospatialIndex("position") );
// prepare data
repo.save( new Location("A", 0.001, -0.002) );
repo.save( new Location("B", 1, 1) );
repo.save( new Location("C", 0.5, 0.5) );
repo.save( new Location("D", -0.5, -0.5) );
repo.save( new Location("Berlin", 13.405838, 52.531261 ));
repo.save( new Location("Cologne", 6.921272, 50.960157 ));
repo.save( new Location("Dsseldorf", 6.810036, 51.224088 ) );
List<Location> locations = repo.findByPositionNear(DUS , new Distance(1, Metrics.KILOMETERS) );
return "Success";
}
“findByPositionNear”适用于
private static final Point DUS = new Point( 13.405838, 52.531261 );
但如果我给Fortworth的坐标,比如说,
private static final Point DUS = new Point( 13.405838, 52.531261 );
失败,但例外情况是:
错误17444传统点超出了球形查询的范围。
有一个我已经研究过的相关问题,它没有足够的信息。
还有其他方法吗?或者任何人都可以解释为什么一个有效而另一个无效?
答案 0 :(得分:0)
正如已经强调的那样,在球形查询上有一个vaildator,findByPositionNear和mongodb $ near函数使用。
尝试
Circle area = new Circle(new Point(13.405838, 52.531261), new Distance(radius, metric));
Location l = locationCrud.findByPositionWithin(area);
Circle没有这样的限制。