How to embed value of let variable from lookup stage of mongodb aggregation?

I’ve created a mongoplayground to make it easier to understand.

First. What do I have?

I have category schema containing ids which refer to filter schema entities.

In filter schema I have a property called “targetProperty” which contains a property name which describes a detail of a product. Product schema has a property details which is key-value map, where the value of “targetProperty” mentioned earlier is the key of this details map. Entities of variations inside product may contain the same details key-value map property as well.

For example if “targetProperty” of filter entity is “memory”, then there is a product entity containing a “details” property which contains “memory” as its key and object {“title”: “Memory”, “value”: “256 GB”} as its value or some of the product variations might contain “memory” property in its details key-value map.

const categorySchema = new Schema<ICategory>({    
  filters: [{ type: Schema.Types.ObjectId, ref: filterModelName }],
  // some other properties unrelated to the issue...
});

const filterSchema = new Schema<IFilter>({  
  targetProperty: {
    type: String,
    required: true
  } 
  // some other properties...
});

const productSchema = new Schema<IProduct>({          
  details: {
    type: Map,
    of: {
      title: { type: String, required: true },
      value: { type: Schema.Types.Mixed, required: true }
    },
    required: true
  },
  variations: [variationSchema],  
  categories: [Schema.Types.ObjectId],
  // some other properties...
});

const variationSchema = new Schema<IProductVariation>({
  details: {
    type: Map,
    of: {
      title: { type: String, required: true },
      value: { type: Schema.Types.Mixed, required: true }
    }
  }
  // some other properties...
});

Second. What is my goal?

With the help of aggregation I want to gather all possible options for relevant filters of a category. So for an example if iPhones category contains a memory filter, I want to iterate over all products which contain iPhone category and gather memory values like “128gb”, “256 gb” and so on they contain under “memory” key of details map property as possible options for memory filter of the iPhone category.

So the final result would look something like:

categories: [
  {
    _id: "0",
      filters: [
         {
          "targetProperty": "memory"
           options: ["128 gb", "256 gb", "512 gb"],
          // ...
        },
       // ...
      ],
      // ...
  }
]

My aggregation pipeline:

db.categories.aggregate([
  {
    "$lookup": {
      "from": "filters",
      "localField": "filters",
      "foreignField": "_id",
      "as": "filters",
      "let": {
        "category_id": "$_id"
      },
      "pipeline": [
        {
          "$lookup": {
            "from": "products",
            "let": {
              "target_property": "$targetProperty"
            },
            "as": "options",
            "pipeline": [
              {
                "$match": {
                  "$and": [
                    {
                      "$expr": {
                        "$in": [
                          "$$category_id",
                          "$categories"
                        ]
                      }
                    },
                    {
                      "$expr": {
                        "$or": [
                          {
                            "$ne": [
                              {
                                // ######## SOURCE OF THE PROBLEM ########
                                "$getField": "details.$$target_property"
                              },
                              null
                            ]
                          },
                          {
                            "$ne": [
                              {
                                // ######## SOURCE OF THE PROBLEM ########
                                "$getField": "variations.details.$$target_property"
                              },
                              null
                            ]
                          }
                        ]
                      }
                    }
                  ]
                }
              },
              {
                "$unwind": {
                  "path": "$variations",
                  "preserveNullAndEmptyArrays": true
                }
              },
              {
                "$group": {
                  "_id": {
                    "$ifNull": [
                      {
                        // ######## SOURCE OF THE PROBLEM ########
                        "$getField": "details.$$target_property.value"
                      },
                      {
                        // ######## SOURCE OF THE PROBLEM ########
                        "$getField": "variations.details.$$target_property.value"
                      }
                    ]
                  }
                }
              },
              {
                "$group": {
                  "_id": null,
                  "options": {
                    "$push": "$_id"
                  }
                }
              },
              {
                "$project": {
                  "_id": 0
                }
              }
            ]
          }
        }
      ]
    }
  }
])

Third. The problem I stumbled on.

Pay your attention on lines under // ######## SOURCE OF THE PROBLEM ######## comment lines of aggregation query above. It seems that value of $lookup aggregation stage “let” variable can’t be embedded the way I tried to do it. And my question is how to embed the value of the let variable correctly so I would be able to dynamically access details property’s nested properties? If this is possible at all…

Thanks for your attention!