Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

like query in php mongo db [duplicate]

Tags:

regex

php

mongodb

I am trying to implement MySQL like query in mongo db with PHP but I don't know how to do that.

select * from stud where name like "%abc%"

php code:

$regex = new \MongoDB\BSON\Regex("^(.*?(\abc\b)[^$]*)i$"); 
$result = $db->stud->find(array('name' => $regex));

I also Tried this

$result = $db->stud->find(( { name : { $regex: '*.abc.*'} } ));
like image 796
Juned Ansari Avatar asked Oct 19 '25 17:10

Juned Ansari


1 Answers

According to this manual to perform a LIKE match you don't need to use wildecards anywhere in pattern. For example to query %abc% you have to do this:

$result = $db->stud->find(array(
    'name' => new \MongoDB\BSON\Regex("abc")
));
like image 190
revo Avatar answered Oct 21 '25 09:10

revo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!