As the Address
is a model for storing the location, we can use it for geospatial indexing! This is done as follows:
class Address include Mongoid::Document field :street, type: String field :zip, type: Integer field :city, type: String field :state, type: String field :country, type: String field :coordinates, type: Array index [[ :coordinates, Mongo::GEO2D ]] belongs_to :location, polymorphic: true end
The indexes need to be created in the model manually. Mongoid will not issue commands to create them unless explicitly told to do so. Let's create indexes as follows:
$ rake db:mongoid:create_indexes
Generated indexes for Address
Generated indexes for Author
Generated indexes for Book
Generated indexes for Category
Not a Mongoid parent model: app/models/lease.rb
Generated indexes for Member
Generated indexes for Order
Not a Mongoid parent model: app/models/purchase.rb
Not a Mongoid parent model: app/models/review.rb