简体   繁体   English

MongoDB嵌套$ or查询

[英]MongoDB Nested $or query

Example of a Mongo Entry: Mongo条目示例:

array(
    'name' => 'blog one',
    'blogCategory' => array(
        'displayAndLightMeasurement' => '1',
        'LEDAndDisplayTestInstrument' => '0'
    )
);

A Query like this works fine: 这样的查询工作正常:

$blogInfoRaw = $collection->find(array('blogCategory' => array('displayAndLightMeasurement' => '1')));

When I try to '$or' query like this: 当我尝试像这样'$or'查询时:

$blogInfoRaw = $collection->find(array('$or' => array('blogCategory' => array('displayAndLightMeasurement' => '1')),array('blogCategory' => array('LEDAndDisplayTestInstrument' => '1'))));

I get this error: 我收到此错误:

$or requires nonempty array

What am I doing wrong? 我究竟做错了什么?

You really meant to use "dot notation" to reference the embedded fields: 您确实打算使用“点符号”来引用嵌入的字段:

$blogInfoRaw = $collection->find(
    array(
       '$or' => array(
           array( 'blogCategory.displayAndLightMeasurement' => '1' ),
           array( 'blogCategory.LEDAndDisplayTestInstrument' => '1')
       )
    )
);

Otherwise the notation you are using implies that the "only" elements present in the embedded level are those that you specify. 否则,您使用的表示法表示嵌入级别中存在的“仅”元素是您指定的元素。 This is not true since there are multiple keys. 这是不正确的,因为有多个键。 So "dot notation" solves this problem by referencing the distinct keys. 因此,“点符号”通过引用不同的键来解决此问题。

PHP array notation does not help here, but the $or needs to be a wrapping "real" array as in [] also. PHP数组表示法在这里无济于事,但是$or也必须是包装的“真实”数组,如[]

Issuing a json_encode often helps when comparing to the official MongoDB examples. 与官方的MongoDB示例相比,发出json_encode通常会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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