使用PHP的MongoDB \ Driver \ Query类在查询中使用正则表达式设置过滤器 [英] Set filter with regex in query with PHP's MongoDB\Driver\Query class

查看:699
本文介绍了使用PHP的MongoDB \ Driver \ Query类在查询中使用正则表达式设置过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP的 MongoDB驱动程序中使用MongoDB 我无法过滤搜索结果带有正则表达式.在本手册中,没有提供如何使用过滤器"选项的示例: MongoDB \ Driver \ Query .

Using MongoDB with PHP's MongoDB driver I am not able to filter search results with regular expressions. In the manual there are no examples given how to use the "filter" option: MongoDB\Driver\Query.

 $manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
 $filter = array(?????);
 $options = array("projection" => array("fieldname" => 1));
 $query = new MongoDB\Driver\Query($filter, $options);
 $cursor = $manager->executeQuery("dbname.collectionname", $query);
 foreach($cursor as $document) {
    var_dump($document);
 }

我尝试了20种不同的可能性,但找不到答案.没有正则表达式的查询可以正常工作.

I tried about 20 different possibilities but can't find the answer. Queries without regular expressions work fine.

推荐答案

我很愚蠢.这个:

'fieldname' => array('$regex' => 'm')

将在字段"fieldname"中找到所有带有"m"的文档.

will find all documents with an "m" somewhere in the field "fieldname".

这篇关于使用PHP的MongoDB \ Driver \ Query类在查询中使用正则表达式设置过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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