Search code examples
mongodbdoctrine-odm

MongoDB $in, $all with regular expressions


i'm working with mongodb and doctrine/mongodb-odm-bundle i'm trying to build a query like this

$matches = array("/^abc/", "/^dbc/);

$this->createQueryBuilder()
    ->field('field_name')->in($matches)
    ->getQuery();

is use $all and $in conditional operators with regular expressions. it's posible?


Solution

  • You can, but while I'm no PHP expert, it looks like $matches is an array of strings, not regular expressions. You'd need to do something like this instead:

    $matches = array(new MongoRegex("/^abc/"), new MongoRegex("/^dbc/"));
    

    In the shell you'd do:

    db.collection.find({field_name: {$in: [/^abc/, /^dbc/]}})