2

我正在使用 spring-data-mongodb(版本 1.0.2.RELEASE)和 mongodb(版本 2.2)。我有一个包含对象位置列表的对象 A。课程如下:

public class A {

@Id
private ObjectId id;

private List<Location> places;

//GETTER AND SETTER
}



public class Place {

private String name;

private String description;

@GeoSpatialIndexed
private double[] location;

//GETTER AND SETTER
}

我需要找到具有特定位置的所有对象 A。我尝试将运算符 $within 和 $elemMatch 一起使用,如下所示:

@Query(value = "{'places' : { $elemMatch: { location: {'$within' : {'$center' : [?0, ?1]} } }}}")
public List<A> findByLocation(Point location, double radius);

当我运行此查询时,我收到以下异常:

org.springframework.data.mongodb.UncategorizedMongoDbException: 找不到特殊索引: 2d for: { places: { $elemMatch: { location: { $within: { $center: [ { x: 41.904159, y: 12.549132 }, 0.07000000000000001 ] } } } } }; 嵌套异常是 com.mongodb.MongoException: can't find special index: 2d for: { places: { $elemMatch: { location: { $within: { $center: [ { x: 41.904159, y: 12.549132 }, 0.07000000000000001 ] } } } } }

有什么建议么?

问候

4

2 回答 2

0

我解决了更改 A 类的问题。我用 org.springframework.data.mongodb.core.geo.Point 列表替换了 Place 列表,如下所示:

public class A {

@Id
private ObjectId id;

@GeoSpatialIndexed
private List<Point> places;

//GETTER AND SETTER
}

我用新查询替换了旧查询,如下所示:

@Query(value = "{'places' : {'$within' : {'$center' : [?0, ?1]} } }")
public List<A> findByLocation(Point location, double radius);

现在它的工作!

谢谢大家!

于 2013-09-04T10:36:50.660 回答
0

显然属性上没有二维索引location,而 MongoDB 需要存在这样的索引。我不知道 Spring 是否应该自己创建该索引,但如果没有,您可以通过

db.collection.ensureIndex({"location":"2d"})

wherecollection替换为您的集合的名称。

于 2013-09-03T19:10:21.543 回答