【问题标题】:Ninjaframework, Morphia and GeoSpatial indexesNinjaframework、Morphia 和 GeoSpatial 索引
【发布时间】:2016-11-09 00:18:18
【问题描述】:

我正在尝试在 MongoDB 中存储和查询地理坐标。我将 Ninjaframework 与 ninja-mongodb 模块一起使用,特别是使用 Morphia。

有坐标的实体是这样的

@Entity
@Indexes({
    @Index(fields = @Field(value="coordinates", type = IndexType.GEO2DSPHERE))
})
public class Place {
    @Id
    public ObjectId id;
    public String google_places_id;
    public String name;
    public Point coordinates;
}

创建对象并查询它们的代码如下所示

try {
        List<models.lc.core.Place> lcPlaces = mongoDB.getDatastore().find(Place.class).field("coordinates").near(lat, lng).asList();
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(   DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        Places places = mapper.readValue(Http.Get(url), Places.class);
        for(models.google.Place place:places.results) {
            models.lc.core.Place p = new models.lc.core.Place();
            p.name = place.name;
            p.coordinates = GeoJson.point(place.geometry.location.lat, place.geometry.location.lng);
            p.google_places_id = place.place_id;
            mongoDB.save(p);
        }
        return new Result(HttpStatus.OK_200).json().render(places);
        //return new Result(HttpStatus.OK_200).text().render(Http.dumpInputStream(Http.Get(url)));
    }
    catch (Exception e){
        Places response = new Places();
        response.status = url;
        return new Result(HttpStatus.BAD_REQUEST_400).json().render(e);
    }

我收到此错误消息

"errorMessage": "error processing query: ns=core-api.PlaceTree:   GEONEAR  field=coordinates maxdist=1.79769e+308 isNearSphere=0\nSort:  {}\nProj: {}\n planner returned error: unable to find index for $geoNear query",
"errorCode": 2,
"message": "Query failed with error code 2 and error message 'error processing query: ns=core-api.PlaceTree: GEONEAR  field=coordinates maxdist=1.79769e+308 isNearSphere=0\nSort: {}\nProj: {}\n planner returned error: unable to find index for $geoNear query' on server localhost:27017",
"localizedMessage": "Query failed with error code 2 and error message 'error processing query: ns=core-api.PlaceTree: GEONEAR  field=coordinates maxdist=1.79769e+308 isNearSphere=0\nSort: {}\nProj: {}\n planner returned error: unable to find index for $geoNear query' on server localhost:27017",
"suppressed": []

我尝试调用 mongoDB.ensureIndexes(true);它确实在坐标上创建索引。但我仍然收到上面粘贴的错误。

互联网没有帮助,我希望有人可以。

非常感谢!

【问题讨论】:

  • 在手动创建索引后它可以工作,所以问题在于注释索引的创建 - 我认为

标签: mongodb geolocation geospatial morphia ninjaframework


【解决方案1】:

你能用 morphia 创建的索引定义和你手动创建的索引定义提交issue 吗?如果 morphia 正在创建错误的索引,我想修复它。谢谢。

【讨论】:

  • 感谢您的回复,我会尽快提出问题。
猜你喜欢
  • 2014-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-05
  • 2015-12-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多