Question

How to convert this script for mongoDB?

select * from table where id > -1 and id in (4,5)

I need to execute this query with two where conditions on the same field.

This doesn't seem to work in PHP:

$db->$col->find(
  array(
    'id' => array( '$gt' => -1 ),
    'id' => array( '$in' => array( 4, 5 ) )
  )
);
Was it helpful?

Solution

Basically the problem is that you have a duplicate key in your array so the latter overwrites the prior so your query is only doing the $in.

Most poeople look to $and to solve this problem however, very few know that actually:

'id'=>array('$gt'=>-1, '$in'=>array(4,5))

Will also solve it. You can just chain your constraints for that field.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top