I'm using Mongoid in my Rails app. Are there any tutorials for how to store and query location data and fetch objects that lie in a certain radius of a location? I've been looking and have no found anything.
Because, like me, I suspect many people learn better from examples, here is one:
Assuming you have created a 2d index on your geospatial Array field (see below for more detail), and let's say your model object is Place and the location field is :loc, a bounding box query could look like this:
# Bounding Box query
Place.where(:loc.within => { "$box" => [ [ 20.73083, 30.99756 ], [ 45.741404, 51.988135 ] ] }).count
In addition to the link posted by Gates VP above, be sure to also read this doc from Moingoid: http://mongoid.org/docs/querying/criteria.html. You will find more examples like the above there.
In addition, you need to ensure your index has been created. Read the following doc from Mongoid, and the corresponding MongoDB Doc:
http://mongoid.org/docs/indexing.html
http://www.mongodb.org/display/DOCS/Geospatial+Indexing
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With