Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP的MongoDB\BSON\Regex-检查多个字段_Php_Mongodb_Mongodb Query - Fatal编程技术网

PHP的MongoDB\BSON\Regex-检查多个字段

PHP的MongoDB\BSON\Regex-检查多个字段,php,mongodb,mongodb-query,Php,Mongodb,Mongodb Query,背景资料 我有一个PHP7Web应用程序,带有Mongo3.4.3后端。只是想看看如何最好地提供一个快速搜索字段,允许用户键入任何字符串,并让系统尝试搜索文档类型中的所有字段并返回匹配项 文档如下所示: { "_id" : ObjectId("582f62aa6b6347a422abcc97"), "department" : "Accounting", "description" : "some test description", "phnum" : "+19991231234", "fax

背景资料

我有一个PHP7Web应用程序,带有Mongo3.4.3后端。只是想看看如何最好地提供一个快速搜索字段,允许用户键入任何字符串,并让系统尝试搜索文档类型中的所有字段并返回匹配项

文档如下所示:

{ "_id" : ObjectId("582f62aa6b6347a422abcc97"), "department" : "Accounting", "description" : "some test description", "phnum" : "+19991231234", "fax" : "", "site" : "MMN" }
我只是在玩文本搜索和正则表达式

问题

在文本搜索中,似乎不能包含通配符。它将只检查精确表达式。 所以我一直在玩正则表达式,它们更灵活。但是,我需要一种方法来检查所有字段中的字符串。。。不止一个

代码

下面是一些目前有效的测试代码,但它只搜索一个字段:

<?php
        $mongo = new \MongoDB\Driver\Manager('mongodb://127.0.0.1:27017');        
        $filter = [
            //'description' => new MongoDB\BSON\Regex( '^account', 'i' )  // description STARTS with.  THis code works.
            //'phnum' => new MongoDB\BSON\Regex( '^\+1567', 'i' )      // ph number STARTS with .  This code works.
            //'phnum' => new MongoDB\BSON\Regex( '0186', 'i' )      // phnumber contains.  This code works as expected.
            'description' => new MongoDB\BSON\Regex( '^account', 'i' )
        ];
        $options = [
            'sort' => array( 'OrderBy' => -1 )
        ];
        $mongoQuery = new MongoDB\Driver\Query( $filter, $options );    
        $cursor = $mongo->executeQuery('widgets.contact', $mongoQuery);
        //var_dump($rows);
        echo "<table><th>Department</th><th>Description</th><th>Phone</th><th>Fax</th><th>Site</th>";
        foreach ($cursor as $document) {
            //var_dump($document);
            echo "<tr>";
            echo "<td>$document->department</td>";
            echo "<td>$document->description</td>";
            echo "<td>$document->phnum</td>";
            echo "<td>$document->fax</td>";
            echo "<td>$document->site</td>";
            echo "</tr>";
        }
        echo "</table>"
?>
问题:

如何更改此代码,使其搜索所有字段-部门、描述等。我可以使用排序的通配符吗?我一直在搜索stackoverflow,但到目前为止我还没有找到答案

任何提示都将不胜感激。

$filter可以包含多个条件。默认情况下,它使用and运算符,但也可以显式使用or。如果单个字段有多个条件,则需要使用并明确:

    $filter = [
        'description' => new MongoDB\BSON\Regex( '^account', 'i' ),
        '$and' => [
            ['phnum' => new MongoDB\BSON\Regex( '^\+1567', 'i' )],
            ['phnum' => new MongoDB\BSON\Regex( '0186', 'i' )],
        ]  
    ];
返回描述以“account”开头、phone以“+1567”开头并包含“0186”的文档

    $filter = [
        '$or' => [
            'description' => new MongoDB\BSON\Regex( '^account', 'i' ),  
            '$and' => [
                ['phnum' => new MongoDB\BSON\Regex( '^\+1567', 'i' )],
                ['phnum' => new MongoDB\BSON\Regex( '0186', 'i' )],
            ]  
        ]
    ];
    $filter = [
        'description' => new MongoDB\BSON\Regex( '^account', 'i' ),
        '$or' => [
            ['phnum' => new MongoDB\BSON\Regex( '^\+1567', 'i' )],
            ['phnum' => new MongoDB\BSON\Regex( '0186', 'i' )],
        ]  
    ];
返回描述以“account”开头的文档,或电话以“+1567”开头并包含“0186”的文档

    $filter = [
        '$or' => [
            'description' => new MongoDB\BSON\Regex( '^account', 'i' ),  
            '$and' => [
                ['phnum' => new MongoDB\BSON\Regex( '^\+1567', 'i' )],
                ['phnum' => new MongoDB\BSON\Regex( '0186', 'i' )],
            ]  
        ]
    ];
    $filter = [
        'description' => new MongoDB\BSON\Regex( '^account', 'i' ),
        '$or' => [
            ['phnum' => new MongoDB\BSON\Regex( '^\+1567', 'i' )],
            ['phnum' => new MongoDB\BSON\Regex( '0186', 'i' )],
        ]  
    ];
返回说明以“account”开头、电话以“+1567”开头或包含“0186”的文档

    $filter = [
        '$or' => [
            'description' => new MongoDB\BSON\Regex( '^account', 'i' ),  
            '$and' => [
                ['phnum' => new MongoDB\BSON\Regex( '^\+1567', 'i' )],
                ['phnum' => new MongoDB\BSON\Regex( '0186', 'i' )],
            ]  
        ]
    ];
    $filter = [
        'description' => new MongoDB\BSON\Regex( '^account', 'i' ),
        '$or' => [
            ['phnum' => new MongoDB\BSON\Regex( '^\+1567', 'i' )],
            ['phnum' => new MongoDB\BSON\Regex( '0186', 'i' )],
        ]  
    ];