title: Filtering a Mongoid collection through relations
body: <p>I'm using Mongoid to build a Rails 4 app.</p>
  
  <p>The problem I'm having right now is how to filter some Mongoid objects through their own relations and have a Mongoid::Criteria at the end instead of an Array.</p>
  
  <p>This is some example code:</p>
  
  <pre><code>class Editor
    include Mongoid::Document
  
    has_many :books
  
    def likes
      books.collect { |book| book.likes }
    end
  end
  
  class Book
    include Mongoid::Document
  
    belongs_to :editor
  end
  </code></pre>
  
  <p>What I would like to be able to do is something like:</p>
  
  <pre><code>Editor.last.likes.where(:created_at.gt =&gt; 10.days.ago)
  </code></pre>
  
  <p>but of course this doesn't work as Editor.last.likes returns an Array, not a Mongoid::Criteria</p>
  
  <p>I know Mongoid has an aggregation framework but it's not entirely clear to me how to use it, nor if it's the best way to solve my problem.</p>
  
  <p>Suggestions?</p>
  
  <p>TIA,
    ngw</p>