检查查找管道中的不存在的字段

发布于 2025-02-07 07:54:51 字数 868 浏览 2 评论 0原文

如何检查字段是否在查找表达式中是否存在?类似的答案问题 (例如{$ eq:null}$已存在:true)。

例如,我想仅当禁用不存在时,我想查找库存

db.orders.aggregate([
  {
    $lookup: {
      from: "inventory",
      let: {item: "$item"},
      pipeline: [
        {
          $match: {
            $expr: {
              $and: [
                {
                  $eq: ["$sku", "$$item" ]
                },
                {
                  $eq: [ "$disabled", null ]
                }
              ]
            }
          }
        },
      ],
      as: "inv"
    }
  }
])

一个游乐场样本为在这里

How to check if a field is not existing inside lookup expression? The answers in similar questions are not helpful (e.g. {$eq : null} or $exists:true).

For example, I want to lookup inventory only if disabled is not existing.

db.orders.aggregate([
  {
    $lookup: {
      from: "inventory",
      let: {item: "$item"},
      pipeline: [
        {
          $match: {
            $expr: {
              $and: [
                {
                  $eq: ["$sku", "$item" ]
                },
                {
                  $eq: [ "$disabled", null ]
                }
              ]
            }
          }
        },
      ],
      as: "inv"
    }
  }
])

A playground sample is here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

長街聽風 2025-02-14 07:54:52

您可以在$ expr之外使用$已存在

db.orders.aggregate([
  {
    $lookup: {
      from: "inventory",
      let: {item: "$item"},
      pipeline: [
        {
          $match: {
            $and: [
              {$expr: {$eq: ["$sku", "$item"]}},
              {disabled: {$exists: false}}
            ]
          }
        }
      ],
      as: "inv"
    }
  }
])

请参阅游乐场示例

You can use $exists outside the $expr:

db.orders.aggregate([
  {
    $lookup: {
      from: "inventory",
      let: {item: "$item"},
      pipeline: [
        {
          $match: {
            $and: [
              {$expr: {$eq: ["$sku", "$item"]}},
              {disabled: {$exists: false}}
            ]
          }
        }
      ],
      as: "inv"
    }
  }
])

See how it works on the playground example

追星践月 2025-02-14 07:54:52

您可以使用:

{ $match: { someField: { $exists: true } } }

在查找之前,要过滤出您不想查找的文档

you could use:

{ $match: { someField: { $exists: true } } }

before the look up, to filter out the documents that you do not want to look up

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文