简体   繁体   English

MongoDB / PHP库:$ regex不起作用

[英]MongoDB / PHP Library: $regex does not work

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. 如果我更改title以匹配数据库中的整个字符串,它可以工作。
I tried the same query with MongoChef and it works. 我尝试了与MongoChef相同的查询,它的工作原理。

Also tried to change the title value with: 还试图用以下方法更改title值:
1. new MongoDB\\BSON\\Regex('Palmer - The ArT oF', 'i'); 1. new MongoDB\\BSON\\Regex('Palmer - The ArT oF', 'i');
2. direct regex string /.*Palmer \\- The ArT oF.*/i 2.直接正则表达式字符串/.*Palmer \\- The ArT oF.*/i
...but still no results. ......但仍然没有结果。

This is the Cursor object of the find() operation: 这是find()操作的Cursor对象:

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. 我正在使用mongo-php-library进行此操作。

We should use '^Hello' instead of /^Hello/'. 我们应该使用'^ Hello'而不是/ ^ Hello /'。 So, the following will work: 因此,以下内容将起作用:

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

That's it. 而已。

Also, when comparing two distinct regex objects the returned result is true. 此外,在比较两个不同的正则表达式对象时,返回的结果为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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM