2012-06-15 55 views
1

我已經嘗試了一些方法來嘗試這樣做,但他們沒有工作:如何在Mongoid中搜索帶有OR語句的嵌入式文檔?

User.where(name:"Jon Sparks").first.purchased_items.where 
      ('(month = 5 AND year = 2012) OR (month = 6 AND year = 2012)') 

User.where(name:"Jon Sparks").first.purchased_items.any_of 
      ({:month => 5, :year => 2012}, {:month => 6, :year => 2012}) 

我想要做的就是把所有purchased_items嵌入文檔中2012分之5或6/2012。

+0

您是否嘗試過? :'User.where(name:「Jon Sparks」)。first.purchased_items.where(:$和=> {:year => 2012},:$或=> {:month => [5,6]}) ' – Anil

+0

我收到以下錯誤:'undefined method'gsub'for:year:Symbol' – Goalie

+0

我認爲你已經在考慮你的條件中的「OR」部分。這真的是一個'IN'。我不是Mongo的專家,所以我會看這個,但我會嘗試'User.where(name:「Jon Sparks」)。first.purchased_items.where(:year => 2012,:month => [5, ('year = 2012 AND month IN(5,6)')' – Anil

回答

0

這是您的查詢

User.where(:name => "JohnSparks").first.purchased_items.where(:year => 2012, :month.in => [5,6])