Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performing an OR of two Mongoid "any_in" queries

If I have two queries that look like:

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

How do I join these into an OR using any_of? I tried this and it doesn't. I tried

Store.any_of({:store_id.any_in => @user.stores_followed}, 
  {:store_id.any_in => @category.stores})
like image 297
Jeremy Smith Avatar asked Sep 30 '11 16:09

Jeremy Smith


2 Answers

It looks like it isn't full supported in Mongoid, so I had to do:

Store.any_of({"store_id" => { "$in" => @user.stores_followed}}, {"store_id" => 
   {"$in" => (:store_id => @category.stores)}})
like image 110
Jeremy Smith Avatar answered Oct 17 '22 15:10

Jeremy Smith


You pass an $or query an array of $in conditions like so:

Store.or( { :store_id.in => @user.stores_followed }, { :store_id.in => @category.stores } )
like image 5
Jesse Clark Avatar answered Oct 17 '22 15:10

Jesse Clark