$expr 查询运算符似乎不适用于数组点表示法 [英] $expr query operator does not seem to work with array dot notation

查看:155
本文介绍了$expr 查询运算符似乎不适用于数组点表示法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在与两个日期之间的比较匹配的集合中查找文档,其中一个在文档中的数组中.

I need to find documents in a collection matching a comparison between two dates, one of them being in an array in the document.

我找到了一个将 $arrayElemAt 与聚合管道结合使用的解决方案,但对这个问题感到沮丧.

I found a solution using a combination of $arrayElemAt with aggregation pipeline but stand frustrated with the problem.

我最初认为问题来自于我如何将 $lte 运算符与 Date 字段一起使用,但我设法将问题缩小到一个更简单的可重现问题.

I originally thought the problem came from how I used the $lte operator with Date fields but I managed to narrow down the issue to a much simpler reproducible one.

db.getCollection('test-expr').insert({
    "value1" : 1,
    "value2" : 1,
    "values" : [ 
        1
    ]
})

现在尝试使用 $expr 查找文档:

Now trying to find the document with $expr:

db.getCollection('test-expr').find({$expr: {$eq: ["$value1", "$value2"]}})

=>返回我刚刚插入的文档.

=> Returns the document I just inserted.

尝试使用点表示法比较数组的第一个元素:

Trying to compare the first element of the array using dot notation:

db.getCollection('test-expr').find({$expr: {$eq: ["$value1", "$values.0"]}})

=>什么都不返回.

我认为点符号可能在 $expr 中不起作用,但我在 MongoDB 文档中找不到任何指出这一点的内容.

I think maybe the dot notation doesn't work in $expr but I couldn't find anything pointing that out in the MongoDB documentation.

如前所述,我已经找到了解决我的问题的解决方案,但想了解为什么不能通过这种方式实现.

As mentioned before, I have found solutions to answer my problem but would like to understand why it cannot be achieved this way.

我错过了什么吗?

推荐答案

$expr 允许使用聚合表达式运算符 only.您使用的点符号将无法访问字段 "values" 的数组元素.: [ 1 ].您需要使用 $arrayElemAt 运算符,它工作正常.

The $expr allows usage of Aggregation Expression Operators only. The dot notation you are using will not work to access the array element for the field "values" : [ 1 ]. You need to use the $arrayElemAt operator, and it works fine.

您的代码 find({$expr: {$eq: ["$value1", "$value2"]}}) 有效,因为 $expr 使用聚合表达式运算符 $eq, 不是 MongoDB 查询语言 (MQL) 的运算符 $eq.请注意,这两个运算符看起来相似,但用法和语法不同.

Your code find({$expr: {$eq: ["$value1", "$value2"]}}) worked, because the $expr used the aggregation expression operator $eq, not the MongoDB Query Language (MQL)'s operator $eq. Note both the operators look alike, but the usage and syntax is different.

而且,代码 find({$expr: {$eq: ["$value1", "$values.0"]}}) 没有像预期的那样工作.在聚合运算符 $values.0 中,0 被解释为字段名称,而不是数组字段的索引.

And, the code find({$expr: {$eq: ["$value1", "$values.0"]}}) did not work - as expected. In the aggregation operator the $values.0, the 0 is interpreted as field name, not an index of an array field.

我想也许点符号在 $expr 中不起作用,但我不能在 MongoDB 文档中找到任何指出这一点的内容.

I think maybe the dot notation doesn't work in $expr but I couldn't find anything pointing that out in the MongoDB documentation.

点符号在 $expr 中也能正常工作.这是一个带有示例文档的示例:

The dot notation works fine in $expr also. Here is an example, with sample document:

{ "_id" : 1, "val" : { "0" : 99, "a" : 11 } }

现在使用 $expr 和点符号:

Now using the $expr and dot notation:

db.test.find({ $expr: { $eq: [ "$val.0", 99 ]  } } )
db.test.find({ $expr: { $eq: [ "$val.a", 11 ]  } } )

两个查询都返回文档 - 匹配发生在使用 $expr 和点符号的过滤器中.但是,这仅适用于嵌入(或子)文档不适用于数组字段.

Both the queries return the document - the match happens with the filter using the $expr and the dot notation. But, this is valid with embedded (or sub) documents only not with array fields.

形成文档,Aggregation Pipeline Operators 说:

这些表达式运算符可用于构造表达式用于聚合流水线阶段.

These expression operators are available to construct expressions for use in the aggregation pipeline stages.

表达式:

表达式可以包括字段路径、文字、系统变量、表达式对象和表达式运算符.表达式可以是嵌套.

Expressions can include field paths, literals, system variables, expression objects, and expression operators. Expressions can be nested.

字段路径

聚合表达式使用字段路径访问输入中的字段文件.要指定字段路径,请在字段名称或虚线字段名称​​(如果该字段在嵌入文档中),带有美元符号 $.例如,$user"指定字段路径用户字段或$user.name"指定user.name"的字段路径领域.

Aggregation expressions use field path to access fields in the input documents. To specify a field path, prefix the field name or the dotted field name (if the field is in the embedded document) with a dollar sign $. For example, "$user" to specify the field path for the user field or "$user.name" to specify the field path to "user.name" field.

这篇关于$expr 查询运算符似乎不适用于数组点表示法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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