Doctrine2 Mongodb 添加更多 $or 运算符

发布于 2024-12-05 02:32:47 字数 458 浏览 1 评论 0原文

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 的主要问题是如何在 $query 中添加添加 $or?

这帮助我使用 $and 运算符,该运算符仍然是丢失的。

我目前正在使用 Symfony2 Doctrine2 Mongodb

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

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 }] });

The main issue here with doctrine2 that I dont understand is how to add addition $or in the $query?

This help me with $and operator which is still missing.

I'm currently using Symfony2 Doctrine2 Mongodb

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏九 2024-12-12 02:32:47
/**
 * 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
 */
/**
 * 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
 */
陌伤浅笑 2024-12-12 02:32:47

理论之外的一个小例子:

使用元语言进行查询:

<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')
            )
    );

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')
            )
    );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文