`$eq` 运算符是否与数组点表示法一起使用? [英] Does the `$eq` operator work with array dot notation?

查看:44
本文介绍了`$eq` 运算符是否与数组点表示法一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 $lookup$match 管道阶段使用 $expr 编写聚合查询.我发现了一些与数组点表示法相关的问题.

I'm trying to write an aggregation query with $expr inside $lookup's $match pipeline stage. I found some problems related to the array dot notation.

想象一个集合 relations 与名为 nodes 的数组字段.

Imagine a collection relations with the array field called nodes.

以下查询可以正常返回正确的结果:

The following query works fine returning correct results:

db.relations.find({"nodes.0": { "type" : "User", "id" : UUID("dc20f7c7-bd45-4fc1-9eb4-3604428fa551") }})

但是下面的那个什么都不返回:

But the one below returns nothing:

db.relations.find({$expr: {$and: [ {$eq: ["$nodes.0", { "type" : "User", "id" : UUID("dc20f7c7-bd45-4fc1-9eb4-3604428fa551") }]}]}})

对我来说,它们似乎实际上是相同的,我不明白为什么结果不同.我尝试使用 $arrayElemAt 运算符,它可以工作,但不幸的是它触发了 COLLSCAN 执行计划,这是不可取的,因为我在 nodes.0 上有一个索引.

To me they seem to be effectively the same, and I don't understand why the results differ. I tried to use the $arrayElemAt operator, it works, but unfortunately it triggers the COLLSCAN execution plan, which is undesirable, as I have an index on nodes.0.

我在文档中没有发现 $eq 运算符有什么特别之处,他们甚至明确提到这两种表达式形式是等效的.

I didn't find anything special about $eq operator in the docs, they even explicitly mention that those two expression forms are equivalent.

推荐答案

$eq 运算符是否与数组点表示法一起使用?

Does the $eq operator work with array dot notation?

没有.当与数组字段一起使用时,$nodes.0 不是 $eq 聚合运算符 中的正确表达式.

No. $nodes.0 is not a correct expression in $eq aggregation operator, when used with array fields.

$eq 聚合运算符 具有以下语法:

{ $eq: [ <expression1>, <expression2> ] }

您必须使用 $reduce$arrayElemAt 来访问 $eq 运算符中的字段:

You will have to use $reduce along with $arrayElemAt to access your field in $eq operator:

查询 1

db.relations.find({
  $expr: {
    $eq: [
      {
        $reduce: {
          input: [
            {
              $arrayElemAt: [
                "$nodes",
                0
              ]
            }
          ],
          initialValue: false,
          in: {
            "$and": [
              {
                $eq: [
                  "$$this.type",
                  "User"
                ]
              },
              {
                $eq: [
                  "$$this.id",
                  UUID("dc20f7c7-bd45-4fc1-9eb4-3604428fa551")
                ]
              }
            ]
          }
        }
      },
      true
    ]
  }
})

游乐场

替代,使用 $eq 运算符 { : { $eq: ;} }

Alternative, using $eq operator { <field>: { $eq: <value> } }

查询 2

db.relations.find({
  "$and": [
    {
      "nodes.0.type": {
        $eq: "User"
      }
    },
    {
      "nodes.0.id": {
        $eq: UUID("dc20f7c7-bd45-4fc1-9eb4-3604428fa551")
      }
    }
  ]
})

MongoDB 游乐场

如果要执行 IXSCAN,可以使用 Query2.您必须创建以下索引:

If you want to perform IXSCAN, you can use Query2. You have to create the following indexes:

db.relations.ensureIndex({"nodes.0.id":1})
db.relations.ensureIndex({"nodes.0.type":1})

这篇关于`$eq` 运算符是否与数组点表示法一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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