Reputation: 75
I'm new to mongo DB and I want to learn how to do a wild car search on a collection. I have a data base like so:
term,value
greenwoods,1
blackberry,2
greenday,3
a Green apple,4
blacksmith,5
and my PHP code is:
<?php
require 'vendor/autoload.php';
$searchterm = "green";
$client = new MongoDB\Client;
$db = $client->database_test;
$wordcollection = $db->table_test2;
$document = $wordcollection->find(
['word' => $searchterm]
);
foreach($document as $doc)
{
var_dump($doc);
}
?>
I want to find all the terms that contain the string green/Green so my desired result is:
greenwoods,1
greenday,3
a Green apple,4
What do I need to change in my code to get the result I mentioned above.
Upvotes: 0
Views: 331
Reputation: 75
I needed to use regex
$document = $wordcollection->find(
'word' => new MongoDB\BSON\Regex($searchterm, 'i'),
);
Upvotes: 1