Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongo combine and or in a single query

I am trying to figure out the best way to query mongo. I am using Mongoid and latest mongo version

What I want to do is query like this

user_id => [array of ids] and user_type => "some_type" OR user_id => [array of ids] and user_type => "some_type"

How can I do it in Mongo/Mongoid?

like image 922
KensoDev Avatar asked Feb 19 '23 06:02

KensoDev


1 Answers

In mongo shell, it would be something like,

db.yourcollection.find({$or: [
    {user_id: {$in: [array_of_ids]}, user_type: "some_type"},
    {user_id: {$in: [array_of_ids2]}, user_type: "some_type2"}
]});
like image 187
Sushant Gupta Avatar answered Mar 03 '23 05:03

Sushant Gupta