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.