首页 > 解决方案 > 新的 MongoDB\BSON\Regex 搜索带有撇号或括号的文本

问题描述

我正在尝试搜索我的 mongo 集合字段以匹配字符串,其中一些带有撇号或括号,如标题:

There's No Way out of Here

我对没有这些字符的字符串的所有搜索都有效。如何指定要匹配的代码?

我的代码:

 $cursor = $collection->find(
    ['$and' => [
        [ 'recording.title'  =>  new MongoDB\BSON\Regex($title, 'i') ],            
        [ 'artist.name'       => new MongoDB\BSON\Regex($artist, 'ig') ]

        ]
    ],
    ['projection'  => [
                        'recording'     => 1,
                        'release'       => 1,
                        'artist'        => 1,
                        'release-group' => 1
                     ],
    ['limit'      => 1 ]]
);

对此的任何帮助将不胜感激!

标签: phpmongodb

解决方案


尝试使用转义这些字符,例如

$title = "There\'s No Way out of Here";

您可以编写PHP代码来转义查询字符串中的所有特殊字符。


推荐阅读