喜欢php mongodb中的查询[重复]

This question already has an answer here:

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.*'} } ));
</div>
dongye1912
dongye1912 致命错误:未捕获MongoDB\Driver\Exception\ConnectionTimeoutException:缺少预期字段“$search”in
2 年多之前 回复
dongxun5349
dongxun5349 不会$db->stud->find(['$text'=>['$name'=>“abc”]])相同/更好,即。全文检索。code.tutsplus.com/tutorials/...,ps不要做“$text”=>[和我一样。
2 年多之前 回复
doudui2229
doudui2229 尝试过,仍然没有找到结果,$result=$db->stud->find(array('name'=>newMongoDB\BSON\Regex('。*abc。*','i')));
2 年多之前 回复

1个回答

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")
));
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问