2011-09-30 55 views
3

如果我有一个看起来像两个查询:执行两个Mongoid“any_in”的或查询

Store.any_in(:store_id => @user.stores_followed) 
Store.any_in(:store_id => @category.stores) 

我如何加入这些成或使用any_of?我试过这个,但没有。 我试图

Store.any_of({:store_id.any_in => @user.stores_followed}, 
    {:store_id.any_in => @category.stores}) 

回答

6

看起来它并不完全在Mongoid支持,所以我不得不这样做:

Store.any_of({"store_id" => { "$in" => @user.stores_followed}}, {"store_id" => 
    {"$in" => (:store_id => @category.stores)}}) 
1

any_in接受使用$in操作值或数组。

ids = @user.stores_followed.map(&:id) + @category.stores.map(&:id) 
Store.any_in(:store_id => ids) 
3

你传递一个$或查询的$数组中的条件,像这样:

Store.or({ :store_id.in => @user.stores_followed }, { :store_id.in => @category.stores })