0

Background Information

I have a PHP 7 web app with a Mongo 3.4.3 back end. Just poking around to see how best to provide a "quick search" field that will allow users to type in any string, and have the system try to search all fields in a document type and return matches.

Documents look like this:

{ "_id" : ObjectId("582f62aa6b6347a422abcc97"), "department" : "Accounting", "description" : "some test description", "phnum" : "+19991231234", "fax" : "", "site" : "MMN" }

I'm just playing around with text searches and also regular expressions.

Problem

With text searches, it seems you cannot include wild cards. It will only check for exact expressions. So I've been playing around with regular expressions, and they are more flexible. However, I need a way to check for a string in ALL fields... not just one.

Code

Here's some test code that currently works, but it searches only one field:

<?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>"
?>

Question

How would I change this code so that it searches all fields - department, description etc. Can I use a wildcard of sorts? I've been searching stackoverflow but so far I haven't found an answer.

Any tips would be appreciated.

0

1 Answer 1

1

$filter can include multiple conditions. By default it uses and operator, but you can also use or explicitly. If you have multiple conditions for a single field, you need to use and explicitly:

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

Returns documents where description starts with 'account', phone starts with '+1567' and contains '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' )],
            ]  
        ]
    ];

Returns documents where description starts with 'account' or phone starts with '+1567' and contains '0186'.

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

Returns documents where description starts with 'account' and phone starts with '+1567' or contains '0186'.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.