Doctrine ODM MongoDB using an OR query

Hold on Cowboy

This blog post is pretty old. Be careful with the information you find in here. It's likely dead, dying, or wildly inaccurate.

This is no where in the documentation that I could find on Doctrine’s site. I wanted to do search with a criteria of OR.

For example, in psuedo code

Find all Events where (first_name like ‘Jim’) OR (last_name like ‘Jim’) OR (title like ‘Jim)

I searched, but the keyword of “OR” with Google doesn’t yield very good results.

Thanks to Jamie Sutherland at http://www.jamiesutherland.com/2011/07/01/doctrine-2-odm-querybuilder-addor/

Here is how it’s done

$result = $this->_dm->createQueryBuilder('HOD\Model\Person');
$result->addOr($result->expr()->field('firstName')->equals(new \MongoRegex("/^{$query}/i")))
    ->addOr($result->expr()->field('lastName')->equals(new \MongoRegex("/^{$query}/i")));
return $result->getQuery()->execute();

Did this help you out? It took me a few days to piece together all this information together, I hope this saves you some time (who knows, maybe the future me will be thankful I wrote this down). Let me know your thoughts. shanestillwell@gmail.com