mongoid/mongoid — Issue #1782: query on nested embedded documents
Author: digitalplaywright
Created: 2012-03-04 17:20:35+00:00
State: open

I'd like to query on an embeds_many collection embedded in an
embeds_many collection, but I am getting
NoMethodError: undefined method `>' for [2]:Array

My intention is to return a document from a top-level embedded
collection if a document in an embedded collection nested within it
match a criteria.

Durran said in a discussion on the mailing list that there is currently
no way to query on a nested embedded collection in Mongoid and
asked me to create this issue.

An example is:

```
# ################
# Models
# ################

class Event
include Mongoid::Document

embeds_many :guests, :class_name => "EventGuest"

end

class EventGuest
include Mongoid::Document

embedded_in :event, :inverse_of => :guests
embeds_many :reservations

end

class Reservation
include Mongoid::Document

field :quantity, :type => Integer

embedded_in :event_guest, :inverse_of => :reservations

end

# ################
# Query
# ################

# given an event with many guests, return a guest with a reservation
of quantity larger than 0:
event.guests.where( "reservations.quantity" => { '$gt' => 0}).first
# => NoMethodError: undefined method `>' for [2]:Array

```

Thank you for looking into this and providing a great tool