php - 新的 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 ]]
);
对此的任何帮助将不胜感激!
解决方案
尝试使用转义这些字符,例如
$title = "There\'s No Way out of Here";
您可以编写PHP
代码来转义查询字符串中的所有特殊字符。
推荐阅读
- git - Visual Studio 2019(社区)和 Git 分支
- javascript - 如何使用 JavaScript 调整图像大小
- python - 有没有更简单的化学分解操作代码?
- javascript - 基于另一个二维数组重新排序二维数组
- java - 如何创建一个最后显示所有名称的程序?
- python - 有没有办法使用 python 脚本故意多次调用 lambda 函数?如何?
- reporting-services - SSRS 仅在报告有差异时运行订阅
- java - 如何在 gcp 应用引擎中部署多个 Spring Boot 服务?
- database - 如何为社交网络应用设置 Firestore?
- javascript - 如何在 Javascript 中使用 async/await - Odoo