sokil / php-mongo

MongoDB ODM. Part of @PHPMongoKit
http://phpmongokit.github.io/
MIT License
242 stars 46 forks source link

Regexp in whereNot expression #148

Closed montekidlo closed 7 years ago

montekidlo commented 7 years ago

Hi,

I found strange behavior in whereNot expression, when I did something like this -

use MongoDB\BSON\Regex;

$expression->whereNot((new Expression())->where('foo', new Regex('bar', '')))

I got error -

Can't have regex as arg to $ne

In code:

        foreach ($expression->toArray() as $field => $value) {
             // $not acceptable only for operators-expressions
            if (is_array($value) && is_string(key($value))) {
                $this->where($field, array('$not' => $value));
            } // for single values use $ne
            else {
                $this->whereNotEqual($field, $value);
            }
        }

Maybe it makes sense to add a check for object, something like this?

if (is_array($value) && is_string(key($value)) || is_object($value)) {

sokil commented 7 years ago

fixed in 1.21.5