Search code examples
phpmongodboperatorsphalconodm

Use mongo operator $and in Phalcon\Mvc\Collection


im trying to use the operator $and of MongoDB applied to the Phalcon\Mvc\Collection as follow:

   $documents = Staff::find( $condition);

with array $condition having the "role" like this:

$condition["role"] = [
            "\$regex" => "$somevalue",  
            "\$and"=> [
                '$ne' => "admin" 
                ]     
            ];   

and i get this error

Can't canonicalize query: Bad Value: unknown operator: $and'

Please help me solve this.
Is there any better way to apply multi conditions to this "role"?


Solution

  • $and is implicty used in mongo by default. Just "$ne" => "admin" should be enough i think.

    MongoDB provides an implicit AND operation when specifying a comma separated list of expressions. Using an explicit AND with the $and operator is necessary when the same field or operator has to be specified in multiple expressions.