How do I perform a $geoIntersects query with Mongoid?

geospatial, mongodb, mongoid, sinatra

Solution

I've been looking at doing the same thing. From what I could see, this isn't supported in Mongoid yet, and I don't know what the timeframe is in terms of it being implemented.

In the meantime, you can use the Mongoid/Moped driver to run the query, but you won't get any of the object mapping niceness that Mongoid provides - you'll just get arrays/hashes back. Example syntax below:

ids = Mongoid.default_session["states"].find( geometry:
    { "$geoIntersects" =>
        { "$geometry" =>
            { type: "Point", coordinates: [-99.176524, 18.929204] }
        }
    }
).select( id: 1 )

This actually returns an array of hashes with a key "_id" and the value of the _id field, but you could configure this as you wish.

Problem

I'm using Sinatra and mongoid driver, now I'm trying to perform this query in mongoid, actually I have a geospatial (Polygon) field called 'geometry': ``` db.states.find({ geometry: { $geoIntersects: { $geometry: { type: "Point", coordinates: [-99.176524, 18.929204] } } } }) ``` Actually this query works in mongodb shell. However, I want to find states that intersects with given point (Point-in-polygon) with mongoid or maybe other ruby driver. Any help would be greatly appreciated. Thanks.

Original source