Find if the point is in the specified MongoDB polygon

I have the following mongoDB document that represents the current position of the vehicle in the GPS field

{
        "_id" : ObjectId("565564e2f3e3f5bd2bdfad18"),
        "vehicleId" : 6521,
        "time" : 1448389800,
        "GPS" : "18.55419790592852,73.80579963326454",
        "readings" : {
                "RPM" : {
                        "value" : 1000
                },
                "Speed" : {
                        "value" : 40
                }
        }
}

I need to find if this entry falls inside the polygon given by the coordinates:

{lat: 18.554321, lng: 73.805231},
{lat: 18.553838, lng: 73.804936},
{lat: 18.553584, lng: 73.806524},
{lat: 18.554240, lng: 73.806546},
{lat: 18.554321, lng: 73.805231}

by specifying the GPS field present in the document.

I tried to execute the following query, but I am not getting any result:

db.data26.find(
   {
     "GPS": {
       $geoWithin: {
          $geometry: {
             type : "Polygon" ,
             coordinates: [ [ [ 73.805231, 18.554321 ], [ 73.804936, 18.553838 ], [ 73.806524, 18.553584 ], [ 73.806546, 18.554240 ], [ 73.805231, 18.554321 ] ] ]
          }
       }
     }
   }
);

Any help would be appreciated

+4
source share
1 answer

The value of your GPS field is a string

"GPS": "18.55419790592852, 73.80579963326454"

Perhaps to use geoWithin your GPS should be an array with the correct order, which [long, lat]looks like this: [73.80579963326454, 18.55419790592852]

+1

Source: https://habr.com/ru/post/1617386/


All Articles