To put it simply I have a list of locations, where each location has it’s own long, lat and radius.
I need to check whether an input long, lat is inside any of the locations, so just comparing the input long, lat if its inside any of the locations sphere.
I am using mongodb, I saw something similar in their documentation, using geoWithin
and centerSphere
, but these work if there its a fixed radius for all locations, where in my case each location has its own radius
This is the provided example in the docs
db.<collection>.find( {
<location field> : {
$geoWithin : {
$centerSphere: [
[ <input longitude>, <input latitude> ],
<fixed radius>
]
}
}
} )
Abdelrahman Ibrahim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
In an aggregation pipeline, you can use $geoNear
to compute distance to the point you specified and store it in a field, say named calculated_distance
. Then you can compare calculated_distance
with your radius to see if the point fall inside your circle.
db.location.aggregate([
{
"$geoNear": {
"near": {
"type": "Point",
"coordinates": [
// your input here
53.23,
67.12
]
},
"distanceField": "calculated_dist"
}
},
{
"$match": {
$expr: {
$lte: [
"$calculated_dist",
"$radius"
]
}
}
}
])
Mongo Playground