How to filter specific field from nested objects using variables?

Hi guys, I have the following document

[
  {
    "category": "task",
    "configuration": {
      "task": {
        "65d65978521ed95587ff6758": {
          "disabled": true,
          "value": "Test"
        }
      },
      "client": {
        "65d68b6266b8c54b4936a09f": {
          "disabled": true
        }
      }
    },
    "templateArray": [
      {
        "k": "task",
        "v": {
          disabled: false,
          value: "Test #5"
        }
      },
      {
        "k": "client",
        "v": {
          disabled: false,
          value: "bar"
        }
      }
    ]
  }
]

My goal is to extract specific fields from configuration based the key value from “templateArray”. For example, if the passed in key value in the aggregation pipeline is “task”, then I would like to extract the object “task” and be able to perform the $objectToArray operation on that. This is what my aggregation pipeline looks like at the moment:

db.collection.aggregate([
  {
    $addFields: {
      foo: {
        "$objectToArray": "$configuration[$category]"
      }
    }
  },
  {
    $addFields: {
      merged: {
        $map: {
          input: "$templateArray",
          as: "template",
          in: {
            k: "$$template.k",
            v: {
              _id: "$$template.v._id",
              name: "$$template.v.name",
              category: "$$template.v.category",
              properties: {
                $map: {
                  input: "$$template.v.properties",
                  as: "property",
                  in: {
                    $mergeObjects: [
                      "$$property",
                      {
                        $arrayElemAt: [
                          {
                            $filter: {
                              input: {
                                $objectToArray: "$configuration[$$template.k]"
                              },
                              as: "config",
                              cond: {
                                $eq: [
                                  {
                                    $toObjectId: "$$config.k"
                                  },
                                  "$$property._id"
                                ]
                              }
                            }
                          },
                          -1
                        ]
                      }
                    ]
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  {
    $project: {
      foo: 1,
      merged: 1
    }
  }
])

The first $addFields operator returns a null, and I’m not sure why. The second $addFields operator is the implementation I’m going for, but doesn’t quite work. I was trying to use $getField operator to extract the fields in configuration, and it works if I explicitly type the field, but doesn’t work when passing in a variable because $getField does not accept a field reference path in this context. Here is a mongodb playground to replicate this issue: Mongo playground.

Any advice is much appreciated!