0

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 ) )
  )
);
6
  • 1
    Stack Overflow is not an alternative to reading the documentation. Did you take a look at the docs? What specifically are you having problems with?
    – Lix
    Sep 17, 2013 at 7:42
  • no documentation for the same field in where i got in there yet .. they just provide example with only one where.
    – kreamik
    Sep 17, 2013 at 7:43
  • 1
    'id'=>array('$gt'=>-1, '$in'=>array(4,5)) tada
    – Sammaye
    Sep 17, 2013 at 7:43
  • please @Sammaye update your as answer so i can make it as answer
    – kreamik
    Sep 17, 2013 at 7:44
  • @Lix : why you just downvote since i don't complete my question yet and the question is posted under 1 minute .. but ah no problem since this forum still help me a lot.
    – kreamik
    Sep 17, 2013 at 7:57

1 Answer 1

0

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.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.