I would like to write a query in a Rails model using mongoid, and I’d like it to return only those records which have embedded child objects (in this case, client work links).
I only want to find clients which have embedded client work links.
This is what I’d like, though obviously it doesn’t work because of the “where” parameters.
def self.latest_client_press
Work.where("!self.work_links.empty?").desc(:updated_at).limit(4)
end
While it is possible in MongoDB to query on array’s size, this feature is rather limited.
What people do instead (and what is recommended on that page) is store array length along with the array itself. This way you can index this field and query documents very efficiently.