Dheemanth Bhat
Dheemanth Bhat

Reputation: 4452

Find if given longitude, latitude pair lies in any of the polygon in MongoDB

As per my current understanding on MongoDB we search our collection by passing an array of [long, lat] pair arrays, to check if that document's [long, lat] pair exits inside the coordinates [[ [], [], [] ]].

i,e.,

db.SomeCollection.find({
    "location":{
        $geoWithin: {
            $geometry: { 
                type: "Polygon", 
                coordinates: [
                    [[1, 2], [1, 3], [1, 4]],
                    [[2, 5], [2, 6], [2, 7]]
                ] }
        }
    }
})

What i am looking for is that i need to feed the [long, lat] pair to a document containing fixed Polygonal Geofence arrays and conclude if the [long, lat] pair exists inside any of those [Polygonal Geofence arrays].

Is this possible in MongoDB? Is my question valid?

Please Note: I am novice to MongoDB as well as Geospatial Data hence forgive if the question contains wrong terminologies.

Upvotes: 0

Views: 675

Answers (1)

Dheemanth Bhat
Dheemanth Bhat

Reputation: 4452

After few days I am returning to my question as i found the answer to it.

The solution is posted in here by Jacob Ribnik.

hence implementing it to my problem it would be something like below:

db.Poly_Geofence_collection.find(
{
    "location": {
        $geoIntersects: {
            $geometry: {
                type: "Point",
                coordinates: [1,1]
            }
        }
    }
});

Where my Poly_Geofence_collection collection looks like:

{
    "_id": ObjectId("SomeRandom12ByteObjectId"),
    "location" : {
        "type" : "Polygon",
        "name" : "Location Name",
        "coordinates" : [
            [
                [0, 0], [1, 2], [1, 1], [2, 1], [0, 0]
            ]
        ]
    }
}

Upvotes: 1

Related Questions