In Aggregation remove element of subarray and delete element of array if empty subarray after remove

Hi to all,
I have a collection with different products that have old codes (not in sale) and new codes (in sale) for same product.
I need to search a collection and add stages to clean if not empty match:

{
  "$match": {
    "products.locations.sizes.productId": "A_S"
  },
  //stages to add to clean response
}

From match result, not null, add aggregation stages to remove objects of subarray sizes with “onSale”: false.
adding another stage to remove locations objects with sizes property empty .
I created playground to test stages solution: Mongo playground with an example of collection.

I can’t modify collection structure to avoid increase works for any site that use it as json format.

Starting from this collection in mongo playgroud:


[
  "_id": {
    "$oid": "68496c8a0bd223e0a0a52c26"
  },
  "products": {
    "locations": [
      {
        "location": "A",
        "latDecDegree": "39",
        "lngDecDegree": "20",
        "sizes": [
          {
            "productId": "S_A_S",
            "onSale": true,
            "locationId": "A",
            "label": "S",
            "size": "S",
            "power": "3",
            "price": "2",
            "document": "/content/P_A_S.pdf"
          },
          {
            "productId": "S_A_M",
            "onSale": true,
            "locationId": "A",
            "label": "M",
            "size": "M",
            "power": "6",
            "price": "3",
            "document": "/content/P_A_M.pdf"
          },
          {
            "productId": "S_A_L",
            "onSale": true,
            "locationId": "A",
            "label": "L",
            "size": "L",
            "power": "9",
            "price": "5",
            "document": "/content/P_A_L.pdf"
          },
          {
            "productId": "A_S",
            "onSale": false,
            "locationId": "A",
            "label": "S",
            "size": "S",
            "power": "3",
            "price": "2",
            "document": "/content/P_A_S.pdf"
          },
          {
            "productId": "A_M",
            "onSale": false,
            "locationId": "A",
            "label": "M",
            "size": "M",
            "power": "6",
            "price": "3",
            "document": "/content/P_A_M.pdf"
          },
          {
            "productId": "A_L",
            "onSale": false,
            "locationId": "A",
            "label": "L",
            "size": "L",
            "power": "9",
            "price": "5",
            "document": "/content/P_A_L.pdf"
          },
          {
            "productId": "S_P_A_S",
            "onSale": false,
            "locationId": "A",
            "label": "S",
            "size": "S",
            "power": "3",
            "price": "2",
            "document": "/content/P_A_S.pdf"
          },
          {
            "productId": "S_P_A_M",
            "onSale": false,
            "locationId": "A",
            "label": "M",
            "size": "M",
            "power": "6",
            "price": "3",
            "document": "/content/P_A_M.pdf"
          },
          {
            "productId": "S_P_A_L",
            "onSale": false,
            "locationId": "A",
            "label": "L",
            "size": "L",
            "power": "9",
            "price": "5",
            "document": "/content/P_A_L.pdf"
          }
        ]
      },
	  {
		"location": "P",
		"latDecDegree": "11",
		"lngDecDegree": "25",
		"sizes": [
			{
				"productId": "P_T_S",
				"onSale": false,
				"locationId": "P",
				"label": "S",
				"size": "S",
				"power": "3",
				"price": "2",
				"document": "/content/P_A_S.pdf"
			},
			{
				"productId": "P_T_M",
				"onSale": false,
				"locationId": "P",
				"label": "M",
				"size": "M",
				"power": "6",
				"price": "3",
				"document": "/content/P_A_M.pdf"
			},
			{
				"productId": "P_T_L",
				"onSale": false,
				"locationId": "P",
				"label": "L",
				"size": "L",
				"power": "9",
				"price": "5",
				"document": "/content/P_A_L.pdf"
			},
			{
				"productId": "S_P_P_T_S",
				"onSale": false,
				"locationId": "P",
				"label": "S",
				"size": "S",
				"power": "3",
				"price": "2",
				"document": "/content/P_A_S.pdf"
			},
			{
				"productId": "S_P_P_T_M",
				"onSale": false,
				"locationId": "P",
				"label": "M",
				"size": "M",
				"power": "6",
				"price": "3",
				"document": "/content/P_A_M.pdf"
			},
			{
				"productId": "S_P_P_T_L",
				"onSale": false,
				"locationId": "P",
				"label": "L",
				"size": "L",
				"power": "9",
				"price": "5",
				"document": "/content/P_A_L.pdf"
			}
		]
      }
    ],
    "cost": "0"
  }
}
]

Thanks a lot for your support.