How can I use regular expressions with Doctrine's Mongodb ODM?

后端 未结 2 858
终归单人心
终归单人心 2021-02-08 05:39

I\'m trying to use regular expressions to query Mongodb using Doctrine\'s Mongodb ODM on Symfony 2.

I know the PHP mongo driver can do it. However, I don\'t know how to

2条回答
  •  忘了有多久
    2021-02-08 06:32

    This came up a while ago on the doctrine-user mailing list. You can use the \MongoRegex class directly in your ODM queries:

    $documentRepository->findBy(array(
        'foo' => new \MongoRegex('/^bar/'),
    ));
    

    Or if using a query builder:

    $queryBuilder->field('foo')->equals(new \MongoRegex('/^bar/'));
    

    Keep in mind that front-anchored, case-sensitive regex patterns will be able to use indexes most efficiently. This is discussed in more detail in the Mongo docs.

提交回复
热议问题