无法通过mongoDB中的动态键访问对象值

时间:2020-03-14 07:12:10

标签: mongodb

我有两个集合,即parent和chilednodes。

父收藏:

{
    "_id" : "5e6c70e8996ddf1c28e14504",
    "startDate" : "2020-02-25T14:01:58.697Z",
    "active_id" : "child_vesrion_1",
    "child_id" : "5e5e2cd4e972a95b6c32b5bf30"
}

chilednodes:

{
    "_id" : "5e5e2cd4e972a95b6c32b5bf30",
    "startDate" : "2020-02-25T14:01:58.697Z",
    "endDate" : null,
    "child_vesrion_1" : {
        "childName" : "xyz",
        "createdDate" : "2020-02-25T14:01:58.697Z",
        "text" : "sometext",
        "type" : "notype"
    },
    "child_vesrion_2" : {
        "childName" : "abc",
        "createdDate" : "2020-02-25T14:01:58.697Z",
        "text" : "sometext2",
        "type" : "notype"
    },
    "active" : "child_vesrion_1"
}

我正在尝试获取父级详细信息以及活动的子版本详细信息。在这里,我正在使用聚合和查找。 我正在使用mongoDB $ addFields构建“ finalList”字段。 这是我的查询;

 db.parent
    .aggregate([
      {
        $match: {
          '_id' : '5e6c70e8996ddf1c28e14504'
        },
      },
      {
        $lookup: {
          from: 'chilednodes',
          localField: 'child_id',
          foreignField: '_id',
          as: 'child',
        },
      },
      {
        $addFields: {
          finalList: {
            $map: {
              input: '$child',
              as: 'c',
              in: {
                $let: {
                    vars: { "currentVersionKey":  "child_vesrion_1"},
                    in: {
                        child_id:'$$c._id',
                        start_date : '$$c.startDate',
                        current_version_Key : '$$currentVersionKey',
                        active_child_name : '$$c.currentVersionKey.childName'
                    }
                }
              },
            },
          },
        },
       },
     {
        $project: {
          child: 0,
        },
      },
    ])

在响应(finalList)中,我需要有效的子名称。我正在使用变量“ currentVersionKey”,并将值分配为“ child_vesrion_1”,然后使用该变量在“ chilednodes”中查找值。我无法获得活跃的孩子名字。

注意:为了进行测试,我已硬编码“ currentVersionKey”。但是会动态填充

这是我的预期结果;

{
    "_id" : "5e6c70e8996ddf1c28e14504",
    "startDate" : "2020-02-25T14:01:58.697Z",
    "active_id" : "child_vesrion_1",
    "child_id" : "5e5e2cd4e972a95b6c32b5bf30",
    "finalList" : [
        {
            "child_id" : "5e5e2cd4e972a95b6c32b5bf30",
            "start_date" : "2020-02-25T14:01:58.697Z",
            "current_version_Key" : "child_vesrion_1",
            "active_child_name" : "xyz"
        }
    ]
}

但是我没有在响应中得到“ active_child_name”:“ xyz”。任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:1)

您可以使用$lookup pipeline

 db.parent.aggregate([
    { $match: { "_id": "5e6c70e8996ddf1c28e14504" } },
    {
        $lookup: {
            from: "childnodes",
            let: { "child_id": "$child_id", "activeid": "$active_id" },
            pipeline: [
                { $match: { "$expr": { $eq: ["$_id", "$$child_id"] } } },
                {
                    $project: {
                        "child_id": "$_id",
                        "start_date": "$startDate",
                        "current_version_Key": "$active",
                        "active_child_name": {
                            "$reduce": {
                                "input": { "$objectToArray": "$$ROOT" },
                                "initialValue": "",
                                "in": {
                                    "$cond": [{ "$eq": ["$$this.k", "$$activeid"] },
                                        "$$this.v.childName",
                                        "$$value"
                                    ]
                                }
                            }
                        }
                    }
                }
            ],
            as: "finalList",
        },
    }
])