I have this query:
User.or( { name: 'John' }, { name: 'Sara' } ).or( { age: 17 }, { age: 18 } ) )
It returns the next Criteria:
#<Mongoid::Criteria
selector: {"enabled"=>true, "$or"=>[{"name"=>"John"}, {"name"=>"Anoun"}, {"age"=>17}. {"age"=>18}]}
options: {}
class: User
embedded: false>
But I want to do 'and' betweend two 'or' that return something like this:
#<Mongoid::Criteria
selector: {"enabled"=>true, "$and"=>[
{"$or"=>[{"name"=>"John"}, {"name"=>"Anoun"}]},
{"$or"=>[{"age"=>17}, {"age"=>18}]}
] }
options: {}
class: User
embedded: false>
How would be the query ?
The find() Method To query data from MongoDB collection, you need to use MongoDB's find() method.
Open Studio 3T and connect to your MongoDB database. Next, open the Import Wizard from the toolbar. Then, choose JSON as the import format. Click OK.
$[] The all positional operator $[] indicates that the update operator should modify all elements in the specified array field. The $[] operator has the following form: { <update operator>: { "<array>.$[]" : value } }
this might help you,
User.where(enabled: true)
.and(
User.or( { name: 'John' }, { name: 'Sara' } ).selector,
User.or( { age: 17 }, { age: 18 } ).selector
)
this will return:
#<Mongoid::Criteria
selector: {"enabled"=>true, "$and"=>[{"$or"=>[{"name"=>"John"}, {"name"=>"Sara"}]}, {"$or"=>[{"age"=>17}, {"age"=>18}]}]}
options: {}
class: User
embedded: false>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With