Вопрос

I need to perform a query based on the value of some boolean fields. These fields may not exist in some documents, as they were added at a later stage.

I tested the query in the shell and worked ok:

db.product.find({$or: [{approved:true},{$and: [{approved:{$exists:false}}, {sold:{$ne:true}}]}]})

But trying to do the same with the PHP driver doesn't seem to work:

$condA = array('approved' => true);
$condB = array('approved' => array('$exists' => false), 'sold' => array('$ne' => true));

$query = array('pid' => $prodId, '$or' => array($condA, array('$and' => $condB)));

I tested some variants but I'm always getting this error in the log:

assertion 13086 $and/$or/$nor must be a nonempty array 

Any hint on what I might be doing wrong? Thanks in advance.

Это было полезно?

Решение

Since multiple "clauses" of a query are interpreted as "and" you don't need the $and in your query. If you take out $and:[ ] you end up with a simpler

{ $or : [ { approved : true }, { approved : {$exists:false}, sold : {$ne:true} } ] }

When you convert that into corresponding PHP that should work for you.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top