質問

I have an arbitary point the represents the position of a house. In my MongoDB I have a collection with polygons representing different areas (similar to zip codes).

I want to return the areas in the collection, where the given point is within that area.

Currently I am converting the point into a "small" box and uses a query like this (which works as expected):

(Point that was boxified: [56.121371, 13.114797])

db.areas.find({
        "area" : {
                "$geoIntersects" : {
                        "$geometry" : {
                                "type" : "Polygon",
                                "coordinates" : [
                                        [
                                                [
                                                        56.120371,
                                                        13.113797
                                                ],
                                                [
                                                        56.120371,
                                                        13.115797
                                                ],
                                                [
                                                        56.122371,
                                                        13.115797
                                                ],
                                                [
                                                        56.122371,
                                                        13.113797
                                                ],
                                                [
                                                        56.120371,
                                                        13.113797
                                                ]
                                        ]
                                ]
                        }
                }
        }
});

I have tried to use $near but it only works with points. I also tried $geoWithin without success; my guess is that it check if the collections documents are within the query, and not if the query is within the documents.

This should be a very common query, so I wonder if there is better way to do this rather then creating a "small" polygon? It feels like a "workaround".

役に立ちましたか?

解決

The $geoIntersects operator allows querying for intersection of two geoJSON objects, and it works with all geometries, including { type: "Point" }

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top