Mais conteúdo relacionado
Semelhante a Geolocalização com MongoDB e Rails (20)
Geolocalização com MongoDB e Rails
- 16. Preparando
$ gem install mongoid --pre
$ gem install bson_ext
Gemfile
gem "mongoid", "2.0.0.beta.16"
gem "bson_ext", "1.0.4"
$ rails generate mongoid:config
- 17. Geospatial Indexing
class Article
include Mongoid::Document
include Mongoid::Timestamps
field :title, :type => String
field :lead, :type => String
field :published_at, :type => Time
field :location, :type => Array
index [[ :location, Mongo::GEO2D ]], :min => 180, :max =>
180
index :published_at, Mongo::DESCENDING
end
db.articles.ensureIndex( { location : "2d"}, { min : -180,
max : 180 } )
- 20. Querying
within box
box = [ [40, 40], [60, 60] ]
Article.where(:location.within => { "$box" => box })
box = [[40, 40], [60, 60]]
db.articles.find({location : {"$within" : {"$box" : box}}}
- 22. Just Build It: You don't need anyone's approval and in
fact, you probably won't get it, so don't even try
Kevin Rose - Digg