Mongo $match命令筛选具有双重嵌套数组的父级

huangapple go评论61阅读模式
英文:

Mongo $match parent having a doubly nested array

问题

I'm trying to $match against Mongo elements for double nested array:

  {
    $match: {
      "cards.[].listings.0": {
        $exists: true,
      },
    },
  },

I'm looking for parent documents which have "cards" which also have "listings".

我正在尝试针对双重嵌套数组在Mongo元素中进行匹配:

  {
    $match: {
      "cards.[].listings.0": {
        $exists: true,
      },
    },
  },

我正在寻找具有"cards"并且也具有"listings"的父文档。

英文:

I'm trying to $match against Mongo elements for double nested array:

  {
    $match: {
      "cards.[].listings.0": {
        $exists: true,
      },
    },
  },

I'm looking for parent documents which have cards which also have listings.

I've seen something like the above syntax before, although I'm not sure what this syntax is called or if it's possible to use it for double-nested arrays.

答案1

得分: 1

在聚合管道中,只需使用:

db.collection.aggregate([
  {
    $match: {
      "cards.listings.0": {
        $exists: true
      }
    }
  }
])

查看在playground示例上的运行情况。

英文:

Inside an aggregation pipeline simply use:

db.collection.aggregate([
  {
    $match: {
      "cards.listings.0": {
        $exists: true
      }
    }
  }
])

See how it works on the playground example

huangapple
  • 本文由 发表于 2023年5月13日 13:51:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76241288.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定