Arrays MongoDB聚合-嵌入式阵列投影中的$filter

Arrays MongoDB聚合-嵌入式阵列投影中的$filter,arrays,mongodb,aggregation-framework,projection,Arrays,Mongodb,Aggregation Framework,Projection,我有一个复杂的JSON对象结构,其中包含(N)个嵌入式数组。单个条目是具有两个字段的对象:@name和content。我需要在投影阶段过滤它们(不使用$unwind),以获得一个特定对象,其中@name字段等于特定值: $characteristics.attributeGroup.attribute输入返回上述结构。我试图在$cond:{$eq:['$$attribute.@name','averageRating']}中使用类似的东西,但它不起作用 你能帮我找到一个解决办法吗 提前谢谢 你

我有一个复杂的JSON对象结构,其中包含(N)个嵌入式数组。单个条目是具有两个字段的对象:
@name
content
。我需要在投影阶段过滤它们(不使用
$unwind
),以获得一个特定对象,其中
@name
字段等于特定值:

$characteristics.attributeGroup.attribute
输入返回上述结构。我试图在
$cond:{$eq:['$$attribute.@name','averageRating']}
中使用类似的东西,但它不起作用

你能帮我找到一个解决办法吗

提前谢谢

你可以试试

  • $reduce
    迭代
    attributeGroup
    数组的循环,并使用
    $mergeObjects
  • 如果属性的类型为
    对象,则检查条件,然后再次检查第二个条件,如果@name匹配,则返回值,否则移动到else条件
  • $reduce
    要迭代
    属性
    数组的循环,请检查名称是否匹配,然后返回值

您是否可以添加一些示例文档而不是图像,以及您希望得到什么样的结果?在object或array中?这是示例json内容:预期结果将只是一个具有该搜索名称的对象,或者至少是一个具有单个条目的数组。e、 g:attribute:{“@name”:“attributeKey2”,“content”:“attributeValue2”}该@name属性值是唯一的。不能在任何其他数组/对象中重复。请添加预期的输出?那会让事情变得容易的,他们是图里维沙尔!谢谢你的回答。我试图将这一阶段应用于更多的“产品”数据。看起来这个结构比我想象的要嵌入得多。我在mongo Playerd中创建了一个新配置:顶层数组是以前的“productAttributes”值,但似乎还有其他attributeGroup数组:/其余的都是相同的,包括对象中的@name和content字段,但经过研究后,我仍然无法在@name字段中提取出一个带有ProduktBasisClass的字段在某些文档中
属性
字段是对象,在某些数组中,是正确的数据,还是
属性
字段同时具有这两种类型的数据?这是正确的。如果有多个属性-它是一个数组,但只有一个-一个json对象:/i我已经更新了答案和游乐场链接,你能检查一下吗。您需要在3个位置传递名称值以进行搜索。正如我在代码中所评论的那样。
 productCategory: {$filter: {
 input: '$characteristics.attributeGroup.attribute',
 as: 'attribute',
 cond: {
        ...?
 }}
let name = "Artikelhinweise";
db.collection.aggregate([
  { $match: { "attributeGroup.attribute.@name": name } }, // name pass here
  {
    $project: {
      attributeGroup: {
        $reduce: {
          input: "$attributeGroup",
          initialValue: {},
          in: {
            $mergeObjects: [
              "$$value",
              {
                $cond: [
                  { $eq: [{ $type: "$$this.attribute" }, "object"] },
                  {
                    $cond: [
                      { $eq: ["$$this.attribute.@name", name] }, // name pass here
                      {
                        "@name": "$$this.attribute.@name",
                        "content": "$$this.attribute.content"
                      },
                      "$$value"
                    ]
                  },
                  {
                    $reduce: {
                      input: "$$this.attribute",
                      initialValue: {},
                      in: {
                        $cond: [
                          { $eq: ["$$this.@name", name] }, // name pass here
                          {
                            "@name": "$$this.@name",
                            "content": "$$this.content"
                          },
                          "$$value"
                        ]
                      }
                    }
                  }
                ]
              }
            ]
          }
        }
      }
    }
  }
])