ryanong / mongoid_spacial

A Mongoid Extention that simplifies and adds support for MongoDB Geo Spacial Calculations.
MIT License
138 stars 63 forks source link

geo on an embedded document #7

Closed mikebaldry closed 13 years ago

mikebaldry commented 13 years ago

Hi, I have a special class called Location, which is embedded in Item.

Item has: embeds_one :collect_from, class_name: "Location"

Location has:

field :point, type: Array, spacial: true field :friendly_name, type: String
embedded_in :item spacial_index :point

When I attempt to do:

Item.where(:"collect_from.point".near(:sphere) => {:point => @location.to_ll, :max => 5, :unit => :mi})

I get the following error:

OperationFailure can't find special index: 2d for: { collect_from.point: { $nearSphere: [ 51.3623448, 0.6053797 ], $maxDistance: 0.001263023073783309 } }

Am I missing something or is this not supported? If not, how should I go about it?

Many thanks!

ryanong commented 13 years ago

you need to create the indexes. rake mongoid:ceate_indexes

ryanong commented 13 years ago

I think you might have to do spacial_index 'collect_from.point'

mikebaldry commented 13 years ago

@ryanong Thanks - Putting the spacial_index 'collect_from.point' on the Item model worked. This means that the spacial functionality isn't encapsulated in the Location class, which is a bit annoying. I've had to include Mongoid::Spacial::Document on the Item as well as the Location, just to get the spacial_index method.

I can't think of a great way of modifying the code to make this use case easier. have you any ideas?

ryanong commented 13 years ago

hrm... I can think of one or two ways but nothing really solid. I'll keep something in mind.