title: Mongoid 2d query on associated model
body: <p>I have two models, <code>Location</code> and <code>Event</code>.</p>
  
  <pre><code>class Event
    include Mongoid::Document
    field :name, type: String
    belongs_to :location
    index({'location.coordinates' =&gt; '2d'}, {unique: true})  # I added this later on because I had an error when querying on location.coordinates
  end
  
  class Location
    include Mongoid::Document
    field :coordinates, type: Array
    index({coordinates: '2d'}, {unique: true})
    has_many :events
  end
  </code></pre>
  
  <p>Now if I create an <code>Event</code> with an associated location</p>
  
  <pre><code>Location.create({coordinates: [40.7127837, -74.0059413]})
  Event.create({name: "foo", location: Location.first})
  </code></pre>
  
  <p>How can I query events near a specific location?
  I tried this:</p>
  
  <pre><code>Event.where('location.coordinates' =&gt; {'$near' =&gt; [40.7127837, -74.0059413], '$maxDistance' =&gt; 1000.fdiv(111.12)}).first
  </code></pre>
  
  <p>but it returns no results, while</p>
  
  <pre><code>Location.where('coordinates' =&gt; {'$near' =&gt; [40.7127837, -74.0059413], '$maxDistance' =&gt; 1000.fdiv(111.12)}).first
  </code></pre>
  
  <p>returns the <code>Location</code> object created earlier.</p>
  
  <p>What am I doing wrong?</p>