Geofencing with MongoDB Geofencing with MongoDB mongodb mongodb

Geofencing with MongoDB


Unfortunately mongodb's geo indexes are for storing point-data only. To store regions would require some serious reworking of the data structure we are using and is a long-term goal, but will not happen in the short term. As a work around, if your regions are roughly similarly sized you might be able to get away with storing the center point of each region. Then you could fetch the ten closest regions and do a client-side filter in your app to figure out which region the query point is in.

Alternatively, you could make an idealized grid and store the grid squares that each region contains in an array for that region. Then map the query point to it's grid square, and figure out which region (if more than one for that square) the point is in. This is actually similar to how we implement our geo index although we use a dynamic grid. You should use a normal (non-geo) index if you want to go this route.

While neither of these are ideal, hopefully they will provide a workable solution.


Why not just use collections for your regions and use polygons for your regions - here's a great presentation from Greg Studer @ 10gen that may give you some ideas: https://docs.google.com/present/edit?id=0ARXrl_iXSUmXZGhieDhqcTdfMTdjcjI4eDNkNQ&hl=en_US