Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 用于元素大小和$elemMatch比较的MongoDB查询数组_Arrays_Mongodb - Fatal编程技术网

Arrays 用于元素大小和$elemMatch比较的MongoDB查询数组

Arrays 用于元素大小和$elemMatch比较的MongoDB查询数组,arrays,mongodb,Arrays,Mongodb,我只需要检索至少有一个格式元素的记录。prices数组和每个元素price都在0到0.99之间: 因此,对于价格比较而言,这很好: { 'formats.prices': { $not: { $elemMatch: { price: { $gt: 0, $lte: 0.99 } } } } } 但是我如何添加像$not:{$size:0}这样的条件来排除完全没有元素的格式。prices { 'formats.prices': { $not: { $elemMatch: { price:

我只需要检索至少有一个
格式元素的记录。prices
数组和每个元素
price
都在0到0.99之间:

因此,对于
价格
比较而言,这很好:

{ 'formats.prices': { $not: { $elemMatch: { price: { $gt: 0, $lte: 0.99 } } } } }

但是我如何添加像
$not:{$size:0}
这样的条件来排除完全没有元素的
格式。prices

{
    'formats.prices': { $not: { $elemMatch: { price: { $gt: 0, $lte: 0.99 } } } },
    'formats.prices.0.price': { $exists: true }
}

这样更好。假设您有一个关于“formats.prices.price”的索引(考虑到您无论如何都想对其进行范围查询,这可能是一个好主意,尽管现在是一个排除),那么这里的
$exists
测试实际上可以使用它,而
$not:{$size:0}
无法使用它。似乎对于正价格,最好进行正匹配
{'formats.prices.price':{$gt:0.99}
,除非您的真正意图是数组中的价格不在该范围内。这实际上是你提出问题的原因。非常感谢@BlakesSeven,很高兴这是最好的选择,因为应用的意图是,我误解了这个问题,因为我需要在这个范围内没有价格下跌(至少一个)。(更新)