0

I am making use of dynamic queries in mongoDB, by using them this way:

$query['clicks'] = array('$gt' => 6);
$query['lang'] = "de";

$cursor = $collection->find($query);

The question is pretty simple and straightforward: how can one add 'OR' conditionals? for example to have both 'lang' to be "de" OR "fr";

Because right now, Mongo implies that there is an AND between "clicks" and "lang". So how can i structure clicks > 6 && ( lang == 'de' || lang == 'fr' )

3 Answers 3

1
          $cond=array();
          $cond=array_merge($cond,array("clicks" => array('$gt' =>6)));
          $cond=array_merge($cond,array("$or" => array(array("lang" =>'de'),array("lang" =>'fr'))));

    $cursor = $collection->find($cond);

----------------------------

    another way is : 
       $cond=array();
          $cond=array_merge($cond,array("clicks" => array('$gt' =>6)));
          $cond=array_merge($cond,array("$in" => array('de','fr')));

    $cursor = $collection->find($cond);
Sign up to request clarification or add additional context in comments.

Comments

0

You can give try as below

$query['clicks'] = array('$gt' => 6);
$query['$or'] = array(array('lang' => 'de'), array('lang' => 'fr'));
$cursor = $collection->find($query);

How to add $in operator

$query['clicks'] = array('$gt' => 6);
$query['$or'] = array(array('lang' => 'de'), array('lang' => 'fr'));
$query['_id'] = array('$in' => array(new mongoId('506d95bc5c73952c14000002'), new mongoId('506d95bc5c73952c14000003')));

$cursor = $collection->find($query);

12 Comments

thank you. you get the right answer for simplicity. Just a quick one tho: $query['$or'] = array(array('lang' => 'fr'), array('lang' => 'de')); $query['$or'] = array(array('_id' => new mongoId('506d95bc5c73952c14000002'))); how can I "add" the second 'or' to the first? the way it is now, it just overwrites it. I know it's out of the scope of the question but, well..
do you want clicks > 6 && ( lang == 'de' || lang == 'fr' || _id=='506d95bc5c73952c14000002') this way now ?
yes, but not exactly that. I would like to know how I can dynamically adding more more ORs (I will need to loop though some IDs and add them. It doesn't really have much to do with the question but, if you can help, it'd be appreciated. that new or doesn't have to be inside the brakets
is it necessary to use OR ? you can use $in operator for list of _id
oh, you're right!! didn't remember there was $in, aha. Thank you for the answer! it was great help :)
|
0

$or is good but not extremely index friendly especially when you start getting into more diverse ranges. A slightly more index friendly method is with $in:

$db->collection->find(array('clicks' => array('$gt' => 6), 
    'lang' => array('$in' => array('fr', 'de'))));

That tends to make more efficient use of indexes at times.

Also note that nested $ors do not currently use indexes however $or can use multiple index plans which means that a query like so:

$db->collection->find(array('clicks' => array('$gt' => 6), '$or' => array(
    array('lang' => 'fr'), arrray('lang' => 'de')
))

Would be able to use multiple indexes but in this case it is useless since it only going over one field so better to use $in.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.