I have a jquery auto complete script that takes a JSON array and displays it in a table (similar to google suggest). As you type it requests data from a php script.
What I'd like to do is query this data from a MongoDB database as the person types, however I need it to match any part of the selected field.
So I have a field called "Name" that has last name and first name in the same field. If I have a record for "John Smith" Jo Joh john and Sm Smi Smit smith should all match that same record.
Using $collection->find(array('Name' => 'John'); doesn't match my John Smith record however. What am I doing wrong?
You would need to use regular expressions for that. Such as
$collection->find(array('Name' => new MongoRegex('/John/i'));
Using just 'John' you are searching for records where Name matches "John" exactly. You can use regular expressions to match arbitrary sub strings, eg:
$collection->find(array("Name" => "/.*John.*/i"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With