The $geoIntersects operator allows querying for intersection of two geoJSON objects, and it works with all geometries, including { type: "Point" }
Best way to find polygons wrapping a given point MongoDB?
質問
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".
解決