简体   繁体   中英

Doctrine MongoDB Query Builder addOr query not returning any results

I'm working on super simple search across multiple fields in a document to see if any of them has a single value. (Note: some fields are using regex to search if value is contained in string). Using query builder I constructed the following.

public function search($value, $limit, $offset=0, $orderby = '', $order='' )
{
    $regexVal =  new \MongoRegex('/^.*(\b'.str_replace(' ', '\s', $value).'\b).*?$/i');

    $query = $this->repository->createQueryBuilder();

    $query->addOr($query->expr()->field('location')->equals($regexVal));
    $query->addOr($query->expr()->field('mappedData.name')->equals($regexVal));
    $query->addOr($query->expr()->field('mappedData.first_name')->equals($regexVal));
    $query->addOr($query->expr()->field('mappedData.last_name')->equals($regexVal));
    $query->addOr($query->expr()->field('mappedData.email')->equals($value));
    $query->addOr($query->expr()->field('email')->equals($value));
    $query->addOr($query->expr()->field('organization')->equals($value));

    $query->limit($limit)
          ->skip($offset);

    if( ! empty($orderby) && $order ){
        $query->sort($orderby, $order);
    }

    return $query->getQuery()->execute();
}

If I dump out the constructed query values I get the following array in this gist. https://gist.github.com/jchamb/04a0400c989cd28b1841 The extra association field in there is being added by a Doctrine Filter.

Through Query builder I don't get any results, however if I construct the query myself and run it in an admin app like genghis, I get the expected single document result.

Actual written mongodb string looks like this. https://gist.github.com/jchamb/ce60829480576a88290d

This project is a zend2 app that was already using doctrine and mongo. I'm not much of an expert with mongo in general so I'm not sure what i'm doing wrong inside of Query Builder that i'm not getting the same result as executing the query directly. I can't find any info on stack or the query builder docs that gives any extra clues for the multiple addOrs syntax either.

Any help or direction would be really appreciated, in the most basic form I need query builder to get a document where association = x and ( field1 = val or field2 = value).

Thanks!

Really unsure what the exact issue was with the above, but after playing around, switching the order of query builder around fixes the problem.

public function search($value, $limit, $offset=0, $orderby = '', $order='' )
{
    $regexVal =  new \MongoRegex('/^.*(\b'.str_replace(' ', '\s', $value).'\b).*?$/i');

    $query = $this->repository->createQueryBuilder()
             ->find()
             ->limit($limit)
             ->skip($offset);

    $query->addOr($query->expr()->field('location')->equals($regexVal));
    $query->addOr($query->expr()->field('mappedData.name')->equals($regexVal));
    $query->addOr($query->expr()->field('mappedData.first_name')->equals($regexVal));
    $query->addOr($query->expr()->field('mappedData.last_name')->equals($regexVal));
    $query->addOr($query->expr()->field('mappedData.email')->equals($value));
    $query->addOr($query->expr()->field('email')->equals($value));
    $query->addOr($query->expr()->field('organization')->equals($value));

    if( ! empty($orderby) && $order ){
        $query->sort($orderby, $order);
    }

    return $query->getQuery()->execute();
}

Would love to still hear some feedback about why this works and the above didn't if anyone know more about the internals of query builder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM