我希望按地理空间索引从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来执行此操作。我的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份文件。蒙古族用户有什么想法吗?谢谢你的时间和帮助。
发布于 2013-10-20 08:21:56
您可以直接将协调器的值传递到查询中:
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);https://stackoverflow.com/questions/19473657
复制相似问题