【发布时间】:2013-12-03 02:52:26
【问题描述】:
我在 MongoDB 中有一些地理空间数据,我想查找靠近用户位置并符合某些条件的地点列表。
这是我的文档结构:
{ "_id" : { "$oid" : "528af043d1a2760efcd2fd2f" }, "city" : "Miami", "name" : "Some Place", "tickets" : 61, "zipcode" : "33142", "type" : "amusement park", "state" : "FL", "location" : [ -80.24, 25.8092 ]}
{ "_id" : { "$oid" : "528af043d1a2760efcd2fd30" }, "city" : "Miami", "name" : "Other place", "tickets" : 15, "zipcode" : "33150", "type" : "theatre", "state" : "FL", "location" : [ -80.2094, 25.8587 ]}
现在我正在尝试通过纬度、经度来查找有门票并且是某种类型的地方。当我只使用带坐标的 NearQuery(不添加查询)时,我得到了结果,但是当我添加查询对象时,我得到的是空列表。
这是我的代码:
public GeoResults<Place> find(double lat, double lon, int tickets, String type) {
Point point = new Point(lon, lat);
Query query = new Query(Criteria.where("tickets").gt(tickets).
and("type").is(type));
NearQuery nearQuery = NearQuery.near(point).maxDistance(new Distance(10, Metrics.KILOMETERS));
nearQuery.query(query);
GeoResults<Place> repoData = repo.findByLocationNear(point, new Distance(10, Metrics.KILOMETERS));
GeoResults<Place> data = mongoTemplate.geoNear(nearQuery, Place.class);
List<Place> testData = mongoTemplate.find(query, Place.class);
return data;
}
从上面的代码,GeoResults 数据没有内容,而 List testData 返回正确的结果(但不使用空间信息)。如果我使用存储库,我可以获得地点列表,但这不考虑其他参数。
提前致谢
【问题讨论】:
标签: spring mongodb geospatial spring-data spring-data-mongodb