Doctrine2 Mongodb添加更多$或运算符

时间:2011-09-17 04:53:55

标签: mongodb doctrine-orm symfony mongodb-php

doctrine2 ODM可以创建以下查询吗?

db.Product.find({ "$or": [ { "name": new RegExp("test*", "i") }, { "tags": new RegExp("public true*", "i") } ], "$or": [{ "public": false, "_id": { "$in": [ ObjectId("4e74121c4fcfa9ff7ac90000"), ObjectId("4e74121c4fcfa9ff7ac80000") ] } }, { "public": true }] });

这里的主要问题是doctrine2,我不明白如何在$查询中添加额外的$?

这有助于我使用$和运算符,但仍然缺失。

我目前正在使用Symfony2 Doctrine2 Mongodb

2 个答案:

答案 0 :(得分:6)

/**
 * 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
 */

答案 1 :(得分:2)

除理论外的一个小例子:

meta-language中的查询:

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

使用Doctrine的QueryBuilder

进行查询
$queryBuilder
    ->addAnd(<condition1>)
    ->addAnd(
        $queryBuilder
            ->expr()
            ->addOr(
                $queryBuilder
                    ->expr()
                    ->field('field1')
                    ->equals('value1')
            )->addOr(
                $queryBuilder
                    ->expr()
                    ->field('field2')
                    ->equals('value2')
            )
    );