0

Below is the document example saved in MongoDB, I saved polygon data as LNG & lat as mentioned in //https://www.mongodb.com/docs/manual/geospatial-queries/. I want to find the list of documents in which the given point is inside the polygon. eg: lang: -118.421871, lat: 33.943645.

the index is created for zones.area.geo.

I tried the solution mentioned in Mongolink. but data not coming, I am new to mongo, so any help is appreciated.

{
  "zoneType": "GEO_FENCE",
  "zones": [
    {
      "id": "5b9ddf55-d45e-4358-8f37-8fddb7d424e8",
      "name": "Los Angeles, CA 90045, United States",
      "area": {
        "geo": {
          "coordinates": [
            [
              [
                -118.427456,
                33.945997
              ],
              [
                -118.418435,
                33.946995
              ],
              [
                -118.417232,
                33.938442
              ],
              [
                -118.426425,
                33.945997
              ],
              [
                -118.427456,
                33.945997
              ]
            ]
          ],
          "type": "Polygon"
        }
      }
    }
  ]
}

below is the aggregation query I used to check point is inside the polygon or not.

 [
  {
    '$match': {
      '$and': [
        {
          'companyId': {
            '$in': [
              new ObjectId('63368dbb50f6df4a67f08695'), '63368dbb50f6df4a67f08695'
            ]
          }
        }, {
          '$or': [
            {
              'zones.area.geo': {
                '$geoIntersects': {
                  '$geometry': {
                    'type': 'Point', 
                    'coordinates': [
                      -118.426253, 33.945641
                    ]
                  }
                }
              }
            }
          ]
        }
      ]
    }
  }
]

please guide me to get this done. Thank you

Arrow
  • 153
  • 1
  • 2
  • 12
  • I don't think you can create a geo index for all the items in the array. You can create it for a specific item, for example the item at index 0. Please share your index here. If the `zones` array can contain more than one item, I would recommend to separate the document so each polygon will have a document – nimrod serok Nov 30 '22 at 07:48
  • Moreover, your point is outside the polygon and you are using `$and` and `$or` wrongly. Do you want to to get a document that matches one of these conditions? – nimrod serok Nov 30 '22 at 07:57
  • Yes, this point lies within the polygon, 33.945641,-118.426253. – Arrow Nov 30 '22 at 08:02

1 Answers1

0

A working query that returns a result is:

db.collection.aggregate([
  {$match: {
    $or: [
       {companyId: {$in: [ObjectId('63368dbb50f6df4a67f08695'), '63368dbb50f6df4a67f08695']}}, 
          {'zones.area.geo': {
            '$geoIntersects': {
              '$geometry': {
                'type': 'Point', 
                'coordinates': [-118.4218888,  33.944760]
            }
          }
        }
      }
    ]
  }}
])

With index: zones.0.area.geo: 2dsphere

The requested point from the question looks like this: enter image description here

nimrod serok
  • 14,151
  • 2
  • 11
  • 33