MongoDB / doctrine:无法嵌套$或$和 [英] MongoDB/doctrine: can't nest $or in $and

查看:78
本文介绍了MongoDB / doctrine:无法嵌套$或$和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在 $和操作中嵌套多个双操作数 $或操作。 这个讨论的结论听起来和我所需要的类似,但是我我无法让它上​​班。这是我正在尝试做的JavaScript:

I'm having trouble nesting multiple two-operand $or operations within an $and operation. The conclusion of this discussion sounds similar what I need, but I'm unable to get it to work. Here's JavaScript of what I'm trying to do:

db.Business.find(
  {
    $and:
      [
        { $or: [{nm: /American/}, {dsc: /American/}] },
        { $or: [{nm: /Mega/}, {dsc: /Mega/}] }
      ]
  }
)

这个工作在MongoDB交互式shell中。

That works in the MongoDB interactive shell.

这里有一些对我来说看起来不错但没有工作的PHP(导致指示无限递归):

And here's some PHP that looks ok to me but doesn't work (causes infinite recursion where indicated):

$q = $doctrineOdm->createQueryBuilder('Business');
foreach (array('American','Mega') as $keyword) {
  $r = new \MongoRegex('/'.$keyword.'/i');
  $q->addAnd(
    $q->addOr($q->expr()->field('nm')->equals($r))
      ->addOr($q->expr()->field('dsc')->equals($r))
  );
}
print_r($q->getQuery()->getQuery()); // infinite recursion
$cursor = $q->getQuery()->execute();

任何想法?

在此插入

推荐答案

在将它添加到 $ q 之前,您需要构建一个单独的子查询。

It sounds like you need to build a separate subquery before adding it to $q.

$ q-> addAnd(...)将立即进行评估,并将其自身添加到 $ q ,但是您希望它等待。

$q->addAnd(...) is evaluated immediately and adds itself to $q, but you want it to wait.

我没有安装此软件包,因此我无法测试,但这只是一个预感。希望它有帮助。

I don't have this package installed so I can't test, but this is just a hunch. Hope it helps.

$q = $doctrineOdm->createQueryBuilder('Business');
foreach (array('American','Mega') as $keyword) {
  $r = new \MongoRegex('/'.$keyword.'/i');
  $q->addAnd(
    $q->expr()->addOr($q->expr()->field('nm')->equals($r))
              ->addOr($q->expr()->field('dsc')->equals($r))
  );
}
print_r($q->getQuery()->getQuery()); // infinite recursion
$cursor = $q->getQuery()->execute();

这篇关于MongoDB / doctrine:无法嵌套$或$和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆