开发者

How to query a collection of embedded mongo documents to extract specific document with a list of criteria?

I am trying to select a collection of document based on the content of their embedded documents.

My model looks l开发者_开发百科ike this:

class box
  embeds_many :items
  field :stuff
end

class item
  field :attrib1
  field :attrib2
  field :array
end

So with this structure I can query with the following to extract a collection of boxes bases on it's item's attributes:

Box.any_in(:'items.array' => [:value1, :value2]).where(:'items.attrib1'=> 'x', :'items.attrib2' => 'y').order_by([:stuff, :asc])

So this query gives me a collection of box that contains items with attributes 1 = x and attributes 2 = y and array that contains value1 or value2

This is all great, but the problem is that I need to tie up all the attributes into 1 item. What I mean is that this query will return me box like this:

 box
 {
    items
    [
       {array => [value1], attrib1 => "x", attrib2 => "z"}
       {array => [value1], attrib1 => "h", attrib2 => "y"}           
    ]
 }

The criteria's of the query are respected because it's true that attrib1 = 'x' and attrib2 = 'y' in that box, but unfortunately not within the same item.

That's what I need, the list of boxes contains items that have all the desired values within the same item.

How can I do that ? I have just no idea ? I hope I made myself clear, I wasn't really sure how to explain my problem

Thanks,

Alex


I found a beginning of an answer here:

http://www.mongodb.org/display/DOCS/Dot+Notation+%28Reaching+into+Objects%29#DotNotation%28ReachingintoObjects%29-Matchingwith%24elemMatch

Now I need to figure out how to do this in mongoid...

Alex

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜