Doctrine2 Mongodb adding more $or operator

前端 未结 2 1469
北海茫月
北海茫月 2021-01-06 19:44

It is possible for doctrine2 ODM to create the following query?

db.Product.find({ \"$or\": [ { \"name\": new RegExp(\"test*\", \"i\") }, { \"tags\": ne         


        
相关标签:
2条回答
  • 2021-01-06 20:04
    /**
     * Adds an "or" expression to the current query.
     *
     * You can create the expression using the expr() method:
     *
     *     $qb = $this->createQueryBuilder('User');
     *     $qb
     *         ->addOr($qb->expr()->field('first_name')->equals('Kris'))
     *         ->addOr($qb->expr()->field('first_name')->equals('Chris'));
     *
     * @param array|QueryBuilder $expression
     * @return Builder
     */
    
    
    
    /**
     * Adds an "and" expression to the current query.
     *
     * You can create the expression using the expr() method:
     *
     *     $qb = $this->createQueryBuilder('User');
     *     $qb
     *         ->addAnd($qb->expr()->field('first_name')->equals('Kris'))
     *         ->addAnd($qb->expr()->field('first_name')->equals('Chris'));
     *
     * @param array|QueryBuilder $expression
     * @return Query
     */
    
    0 讨论(0)
  • 2021-01-06 20:22

    A small example in addition to theory:

    Query in meta-language:

    <condition1> AND (<field1 == value1> OR <field2 == value2>)
    

    Query with Doctrine's QueryBuilder:

    $queryBuilder
        ->addAnd(<condition1>)
        ->addAnd(
            $queryBuilder
                ->expr()
                ->addOr(
                    $queryBuilder
                        ->expr()
                        ->field('field1')
                        ->equals('value1')
                )->addOr(
                    $queryBuilder
                        ->expr()
                        ->field('field2')
                        ->equals('value2')
                )
        );
    
    0 讨论(0)
提交回复
热议问题