我需要一点帮助,我试图从嵌套数组中提取一些字段,但仍在挣扎,有人能帮忙吗?

以下是示例文档:

[
{
_id: 1,
"_a": [
  {
    "_p": [
      {
        _pubId: {
          "CC": "EN"
        },
        _prid: "2",
        s: {
          c: {
            text: [
              {
                secId: "4"
              },
              {
                secId: "5"
              }
            ]
          },
          d: {
            text: [
              {
                secId: "6"
              },
              {
                secId: "7"
              }
            ]
          }
        }
      },
      {
        _pubId: {
          "CC": "CZ"
        },
        _prid: "4",
        s: {
          c: {
            text: [
              {
                secId: "8"
              },
              {
                secId: "9"
              }
            ]
          }
        }
      }
    ]
  },
  {
    "_p": [
      {
        _pubId: {
          "CC": "CZ"
        },
        _prid: "200",
        s: {
          c: {
            text: [
              {
                secId: "4"
              },
              {
                secId: "5"
              }
            ]
          },
          d: {
            text: [
              {
                secId: "6"
              },
              {
                secId: "7"
              }
            ]
          }
        }
      },
      {
        _pubId: {
          "CC": "BG"
        },
        _prid: "4",
        s: {
          c: {
            text: [
              {
                secId: "8"
              },
              {
                secId: "9"
              }
            ]
          }
        }
      }
    ]
  }
 ]
}
]

而我只需要为"_a.P.S._pubId.CC":"en"提取所有的"_a.P.S.[d|c].ext.secID",输出如下所示:

_prid , secId
{ _prid:2 , secId:4 }
{ _prid:2 , secId:5 }
{ _prid:2 , secId:6 }
{ _prid:2 , secId:7 }

Example playground

推荐答案

See Below,用于基于来自OP的注释更新的聚合管道.

这相当难看,需要在更大的集合上进行测试,但这似乎可以输出您想要的结果.

db.collection.aggregate([
  {"$unwind": "$_a"},
  {"$unwind": "$_a._p"},
  {
    "$match": {
      "_a._p._pubId.CC": "EN"
    }
  },
  {
    "$group": {
      "_id": "$_a._p._prid",
      "pridOut": {
        "$push": {
          "$reduce": {
            "input": {"$objectToArray": "$_a._p.s"},
            "initialValue": [],
            "in": {
              "$concatArrays": [
                "$$value",
                {
                  "$reduce": {
                    "input": "$$this.v.text",
                    "initialValue": [],
                    "in": {"$concatArrays": ["$$value", ["$$this"]]}
                  }
                }
              ]
            }
          }
        }
      }
    }
  },
  {
    "$set": {
      "pridOut": {
        "$reduce": {
          "input": "$pridOut",
          "initialValue": [],
          "in": {"$setUnion": ["$$value", "$$this"]}
        }
      }
    }
  },
  {
    "$set": {
      "pridOut": {
        "$map": {
          "input": "$pridOut",
          "as": "eachSecId",
          "in": {"$mergeObjects": [{"_prid": "$_id"}, "$$eachSecId"]}
        }
      }
    }
  },
  {"$unwind": "$pridOut"},
  {"$replaceWith": "$pridOut"}
])

使用您的样例集合的输出:

[
  {"_prid": "2", "secId": "4"},
  {"_prid": "2", "secId": "5"},
  {"_prid": "2", "secId": "6"},
  {"_prid": "2", "secId": "7"}
]

试穿一下mongoplayground.net号.

下图为:-)

避免使用"$unwind"(直到最后),下面是另一个生成所需输出的聚合管道.

db.collection.aggregate([
  {
    "$match": {
      "_a._p._pubId.CC": "EN"
    }
  },
  {
    "$project": {
      "_id": 0,
      "output": {
        "$reduce": {
          "input": {
            "$reduce": {
              "input": {
                "$reduce": {
                  "input": "$_a._p",
                  "initialValue": [],
                  "in": {"$concatArrays": ["$$value", "$$this"]}
                }
              },
              "initialValue": [],
              "in": {
                "$concatArrays": [
                  "$$value",
                  {
                    "$cond": [
                      {"$eq": ["$$this._pubId.CC", "EN"]},
                      {
                        "$map": {
                          "input": {"$objectToArray": "$$this.s"},
                          "as": "s",
                          "in": {
                            "$map": {
                              "input": "$$s.v.text",
                              "as": "text",
                              "in": {
                                "$mergeObjects": [{"_prid": "$$this._prid"}, "$$text"]
                              }
                            }
                          }
                        }
                      },
                      []
                    ]
                  }
                ]
              }
            }
          },
          "initialValue": [],
          "in": {"$concatArrays": ["$$value", "$$this"]}
        }
      }
    }
  },
  {"$unwind": "$output"},
  {"$replaceWith": "$output"}
])

mongoplayground.net号上试试这件.

Mongodb相关问答推荐

Mongo如何找到字段排除特殊字符

用其他集合中的文档替换嵌套文档数组中的值

MongoDB 聚合 $match 阶段与条件查询

MongoDB 将 JSON 字符串转换为数组[{obj1},{obj2}]的实际对象

更新 Mongodb 中的多嵌套数组

如何根据_id删除文档?

聚合/元素子文档作为 mongo 中的顶级文档

Mongo 无法启动

Mongoid 有 Map/Reduce 吗?

如何在 MongoDB 集合中查找与给定条件匹配的文档和单个子文档

从 mongodb 中的副本配置重新开始

如何在 Mongoose 中更新数组值

如何确保基于特定字段的数组中的唯一项 - mongoDB?

在 mongoDB 中展平嵌套的 JSON struct

MongoDB count() 未定义

用于嵌入式集合的 MongoDB 首选模式.文档与数组

Mongo: query by key one level deep

有人在 Google App Engine 上try 过 MongoDB 吗?

如何使用 mongoose 连接到 mongoDB Atlas

错误:需要数据和盐参数