php - MongoDB/PHP 库 : $regex does not work

标签 php regex mongodb

没有错误,也没有结果。 我的查询如下所示:

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

                )

        )

)

如果我更改 title 以匹配数据库中的整个字符串,它就可以工作。
我用 MongoChef 尝试了相同的查询,它有效。

还尝试通过以下方式更改 title 值:
1. new MongoDB\BSON\Regex('Palmer - The ArT oF', 'i');
2. 直接正则字符串 /.*Palmer\- The ArT oF.*/i
...但仍然没有结果。

这是 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
)

我正在使用 mongo-php-library对于这个操作。

最佳答案

我们应该使用 '^Hello' 而不是/^Hello/'。因此,以下将起作用:

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

就是这样。

关于php - MongoDB/PHP 库 : $regex does not work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35767528/

相关文章:

regex - 如何使用 RegEx 获取 Ini 文件中的节名称

Python、MongoDB、Mongoengine - 类型错误 : string indices must be integers, 不是 str

php - Mysql 查询显示两列之间的结果

php - 对具有不同服务参数的多个实体使用相同的实体监听器

python - 从字符串中删除单词的第一次出现?

regex - 用于匹配 IP 地址的 Python 正则表达式逻辑

javascript - 从 MongoDB 查询中获取数据(在 Auth0 规则中)

mongodb - 文档数据库 |蒙戈错误: aggregation stage not supported: '$lookup on multiple join conditions and uncorrelated subquery

php - 使用php从mysql编码或解码字符串

php - session 中有多个值,但只有第一个值会发送到数据库