3

There are no errors & also no results. My query looks like this:

Array
(
    [$or] => Array
        (
            [0] => Array
                (
                    [title] => Array
                        (
                            [$regex] => /.*Palmer \- The ArT oF.*/
                            [$options] => i
                        )

                )

        )

)

If I change title to match the entire string from database, it works.
I tried the same query with MongoChef and it works.

Also tried to change the title value with:
1. new MongoDB\BSON\Regex('Palmer - The ArT oF', 'i');
2. direct regex string /.*Palmer \- The ArT oF.*/i
...but still no results.

This is the Cursor object of the find() operation:

MongoDB\Driver\Cursor Object
(
    [cursor] => Array
        (
            [stamp] => 0
            [is_command] => 
            [sent] => 1
            [done] => 1
            [end_of_event] => 1
            [in_exhaust] => 
            [has_fields] => 
            [query] => stdClass Object
                (
                    [$query] => stdClass Object
                        (
                            [$or] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [title] => stdClass Object
                                                (
                                                    [$regex] => /.*Palmer \- The ArT oF.*/
                                                    [$options] => i
                                                )

                                        )

                                )

                        )

                )

            [fields] => stdClass Object
                (
                )

            [read_preference] => Array
                (
                    [mode] => 1
                    [tags] => Array
                        (
                        )

                )

            [flags] => 0
            [skip] => 0
            [limit] => 0
            [count] => 1
            [batch_size] => 0
            [ns] => laravel.movie_list
        )

    [server_id] => 1
)

I'm using the mongo-php-library for this operations.

2 Answers 2

1

We should use '^Hello' instead of /^Hello/'. So, the following will work:

$filter = ['NAME' => new MongoDB\BSON\Regex('^Hello', 'i')]; // Works
$cursor = $collection->find($filter);

That's it.

Sign up to request clarification or add additional context in comments.

Comments

0

Also, when comparing two distinct regex objects the returned result is true.

$a = new Regex('a', 'i');
$b = new Regex('b', 'i');
var_dump($a == $b); // will return true
var_dump($a === $b); // will return false

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.