Mongodb \ BSON \ Regex Php:执行类似匹配 [英] Mongodb\BSON\Regex Php: Perform Like Match

查看:37
本文介绍了Mongodb \ BSON \ Regex Php:执行类似匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了以下链接:

https://docs.mongodb.com/php-library/current/tutorial/crud/#regular-expressions https://docs.mongodb.com /manual/reference/operator/query/regex/#perform-a-like-match

在mongo终端正则表达式"/giov/i"上找到:

On mongo terminal regex "/giov/i" Found:

  • Mariogiovanni
  • 乔瓦尼

$cursor = $collection->find([
    'description' => new MongoDB\BSON\Regex(' /giov/', 'i'),

]);

什么也没有回来.为什么?我尝试在该社区找到一些解决方案,但是什么也没有

nothing coming back. Why? I try some solution find on this community but nothing

推荐答案

删除正则表达式定界符,因为它们未在MongoDB\BSON\Regex中使用:

Remove the regex delimiters since they are not used in MongoDB\BSON\Regex:

$cursor = $collection->find([
    'description' => new MongoDB\BSON\Regex('giov', 'i'),

]);

如果您遵循第一个参考,答案是非常明显的链接:

以下示例列出了zips集合中的文档,其中城市名称以"garden"开头,而州为德克萨斯州.

The following example lists documents in the zips collection where the city name starts with "garden" and the state is Texas:

<?php

$collection = (new MongoDB\Client)->test->zips;

$cursor = $collection->find([
    'city' => new MongoDB\BSON\Regex('^garden', 'i'),
    'state' => 'TX',
]);

foreach ($cursor as $document) {
   printf("%s: %s, %s\n", $document['_id'], $document['city'], $document['state']);
}

这篇关于Mongodb \ BSON \ Regex Php:执行类似匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆