Jenssegers / laravel-mongodb regex where does not work

I am working on a Laravel API with a MongoDB database using jenssegers / laravel-mongodb .

I am trying to create a filter to get certain data using a regular expression. In the tutorial for this plugin, I found this:

User::where('name', 'regex', new MongoRegex("/.*doe/i"))->get();

So my code looks like this:

School::where('name', 'regex', new MongoRegex("/haags/i"))->get();

But the result is empty. When I print the query, it looks like this:

db.schools.find({"name":{"$regex":{"regex":"haags","flags":"i"}}})

And when I use this request in the console, it says:

error: {
    "$err" : "Can't canonicalize query: BadValue $regex has to be a string",
    "code" : 17287
}

I also tried:

School::where('name', 'regexp', "/haags/i")->get();

But this gave me this request:

db.schools.find({"name":{"$regex":"\/haag\/i"}})

which apparently avoids the slash and renders the regular expression invalid. Also, the regex should not be between quotation marks or there should be something like this (found in the MongoDB manual):

db.products.find( { description: { $regex: /^S/, $options: 'm' } } )

, MongoDB, - . -, , , ?

+4
1

: compileWhereBasic Builder.

where 3 ,

{$ regexp = > {$ regexp: "a", $options: "i" }} "$ regexp" . = ( ) regexp , .

:

if (! isset($operator) or in_array($operator, ['=','regex']) ) {
        $query = [$column => $value];
    } elseif (array_key_exists($operator, $this->conversion)) {
        $query = [$column => [$this->conversion[$operator] => $value]];
    } else {
        $query = [$column => ['$' . $operator => $value]];
    }

:

if (! isset($operator) or $operator == '=') {
        $query = [$column => $value];
    } elseif (array_key_exists($operator, $this->conversion)) {
        $query = [$column => [$this->conversion[$operator] => $value]];
    } else {
        $query = [$column => ['$' . $operator => $value]];
    }

, !

0

All Articles