问题
I want to do the case insensitive check for the query. previously I am using MongoRegex as describe in the this ticket
But as per the Php.net this is now deprecated, so can't use it, they suggest to use MongoDB\BSON\Regex but I am not sure how use this in symfony2. but it throws class not found exception if I try to use it.
Please let me know the possible solution. Or any other way to do the case insensitive check in the ODM custom query (may be in field function)
Thank you so much.
回答1:
I have written custom query as follows :
$name = 'abc'
$db->createQuerBuilder()
->distinct('username')
->field('username')->where('function(){ var pattern = /^' . $name . '$/i; return pattern.test(this.username); }')
->getQuery()
->execute()
->getSingleResult();
I have used mongodbs where function with JavaScript test function. Reference link
回答2:
If using MongoDB\BSON\Regex
yields "Class Not found" exception then you're using legacy driver and need to use \MongoRegex
. As you've seen the whole driver is deprecated but you can still use it, also it's still the only one ODM officially supports (new driver can be used if combined with polyfill such as mongo-php-adapter).
So to answer your question, case insensitive search will look like (given $qb
is a query builder):
$qb->field('something')->equals(new \MongoRegex("/regex/i"));
来源:https://stackoverflow.com/questions/36761669/symfony2-need-to-use-the-regular-expression-in-doctrines-mongodb-odm-to-check