MongoDB+Doctrine How to use find with pattern matc

2019-07-29 12:29发布

问题:

I am using Mongo DB with doctrine. I want to do pattern matching like below

db.user_logs.find({ "recipientName": /user1/i })

without using MongoRegex class

$qb->field('recipientEmail')->equals(new \MongoRegex('/.*'.$textToSearch.'.*/i'));

purpose is to reach solution without creating class (if possible)

回答1:

Not sure this is the best way, but it works:

$qb->where("function(){ var patt = /".$textToSearch."/i; return patt.test(this.recipientEmail);}");