0
votes

I have problems regarding a Polygone Search in my MongoDB.

I have a document structure like:

Object:{id,type,...,
    data:{
    name,
    loc:{
    lng:xxx
    lat:yyy
    type:Point}}}

I have an 2d Index on "data.loc".

My query in java code is:

DBCollection coll = database.getCollection(type);

    BasicDBList points = new BasicDBList();
    points.add(bbox.getNe());
    points.add(bbox.getSe());
    points.add(bbox.getSw());
    points.add(bbox.getNw());
    points.add(bbox.getNe());
    BasicDBList parentList = new BasicDBList();
    parentList.add(points);

    DBObject query = new BasicDBObject("data.loc",
            new BasicDBObject("$geoWithin",
                    new BasicDBObject("$geometry", new BasicDBObject("type","Polygon")
                            .append("coordinates", parentList))));

The Debuger tells me that the query is for example

{ "data.loc" : { "$geoWithin" : { "$geometry" : { "type" : "Polygon" , "coordinates" : [ [ [ 48.240553 , 16.451597] , [ 48.162751 , 16.451597] , [ 48.162751 , 16.303968] , [ 48.240553 , 16.303968] , [ 48.240553 , 16.451597]]]}}}}

But after typing

DBCursor cursor = coll.find(query);

 try {
        while(cursor.hasNext()) {
            data.add(cursor.next().get("data"));
        }
 } finally {
        cursor.close();
 }

    return data;ยด

data is allways null.

Can anyone find any problems in my approach or does maybe someone have a better approach? In fact I want to do a boundingbox search on my database.

Thank you for your help! Best regards Daniel

1
Can you give an example of one of your documents that you expect to be found by the geo query? Also, to clarify, it's a 2d index and not a 2d sphere index? - wdberkeley

1 Answers

0
votes

Okay, so the Java MongoDB driver query for the points in the polygone for a GeoJson strcuture like this is

  BasicDBList points = new BasicDBList();
    points.add(bbox.getNe());
    points.add(bbox.getSe());
    points.add(bbox.getSw());
    points.add(bbox.getNw());
    points.add(bbox.getNe());
    BasicDBList parentList = new BasicDBList();
    parentList.add(points);

    Set<Object> data = new CopyOnWriteArraySet<Object>();

    DBObject query = new BasicDBObject("geometry",
            new BasicDBObject("$geoWithin",
                    new BasicDBObject("$geometry", new BasicDBObject("type","Polygon")
                            .append("coordinates", parentList))));
    System.err.println(query);