Skip to content

Commit a876fea

Browse files
committed
改进Mongo的Like查询
1 parent 9fabbbb commit a876fea

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/db/builder/Mongo.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,14 @@ protected function parseWhereItem(Query $query, $field, $val): array
288288
// 表达式查询
289289
$result['$where'] = $value instanceof Javascript ? $value : new Javascript($value);
290290
} elseif ('like' == $exp) {
291-
// 模糊查询 采用正则方式
292-
$result[$key] = $value instanceof Regex ? $value : new Regex($value, 'i');
291+
if ($value instanceof Regex) {
292+
//采用正则表达式
293+
$result[$key] = $value;
294+
} else {
295+
// 转义正则特殊字符
296+
$value = preg_quote($value, '/');
297+
$result[$key] = new Regex($value, 'i');
298+
}
293299
} elseif (in_array($exp, ['nin', 'in'])) {
294300
// IN 查询
295301
$value = is_array($value) ? $value : explode(',', $value);

0 commit comments

Comments
 (0)