2014-07-09 61 views
0

如何寫這個mongodb使用java的shell查詢?使用java的mongodb shell查詢

查詢:db.building_feature.find({geometry : {$geoIntersects :{$geometry :{type : "Polygon", coordinates :[[[37.59777,55.73216],[37.59805,55.77615],[37.68710,55.77643],[37.68517,55.73290],[37.59777,55.73216]]]}}}})

我收藏GeoSpatialIndex是 「2dsphere」。

我的版本,它不工作:

DBCollection testCollection = db.getCollection("building_feature"); 
final LinkedList<double[]> geo = new LinkedList<>(); 
geo.addLast(new double[]{37.59777, 55.73216}); 
geo.addLast(new double[]{37.59805, 55.77615}); 
geo.addLast(new double[]{37.68710, 55.77643}); 
geo.addLast(new double[]{37.68517, 55.73290}); 

final BasicDBObject query 
    = new BasicDBObject("geometry", new BasicDBObject("$within", new BasicDBObject("$geometry", geo))); 
System.out.println("Result Count : " + testCollection.find(query).count()); 

感謝。

回答

1

你應該在編寫Java完全相同的查詢:

BasicDBObject polygon = new BasicDBObject("type", "Polygon"); 
polygon.put("coordinates", <myArray>); 

BasicDBObject query = new BasicDBObject(
    "geometry", new BasicDBObject(
     "$geoIntersects", new BasicDBObject(
      "$geometry", polygon 
     ) 
    ) 
); 

<myArray>是您的三重嵌套double[][][] GeoJSON的多邊形座標數組。

順便說一句,它可以更清楚地閱讀一個JSON字符串,然後JSON.parse

而且我覺得$whithin運營商不MongoDB中存在...

+0

謝謝,你可以給例如「GeoJSONPolygonBasicDBObject」? –

+0

看到我的新代碼,我加了'polygon'。 – dgiugg

+0

嗯,這是基本的Java,我認爲這是出於這個問題的目的,它涉及MongoDB API for Java。你不想讓我編寫你所有的代碼,我相信Google會告訴你如何用Java創建數組;) – dgiugg