我希望通过地理空间索引从MongoDB集合中获取文档列表。我已经通过2dsphere索引了该集合
db.getCollection("Info").ensureIndex(new BasicDBObject("location", "2dsphere"), "geospatial");
Info集合中的文档如下所示
{ "_id" : ObjectId("52631572fe38203a7388ebb5"), "location" : { "type" : "Point", "coordinates" : [ 144.6682361, -37.8978304 ] }
当我通过坐标[144.6682361,-37.8978304]查询Info集合时,我收到零集合。
我正在使用JAVA API来执行操作。我的JAVA代码在
之下DBCollection coll1=db.getCollection("Info");
BasicDBObject locQuery = new BasicDBObject();
locQuery.put("near", loc);
locQuery.append("maxDistance", 3000);
locCursor =coll1.find(locQuery);
System.out.println("LOCCURSOR"+locCursor.size());
locCursor.size()始终返回0.不确定我遗失的位置。与此同时,我没有收到任何错误。它只给我0个文件返回。任何想法Mongo用户?感谢您的时间和帮助。
答案 0 :(得分:3)
您可以直接将坐标的值传递到查询中:
double lat_lng_values[] = {144.6682361, -37.8978304};
BasicDBObject geo = new BasicDBObject("$geometry", new BasicDBObject("type","Point").append("coordinates",lat_lng_values));
BasicDBObject filter = new BasicDBObject("$near", geo);
filter.put("$maxDistance", 3000);
BasicDBObject locQuery = new BasicDBObject("location", filter);
System.out.println(locQuery);