Merge incoming data onto already existing data in aggregation pipeline

I am trying to do a lookup on a collection and join the incoming data directly onto my existing document. The existing document has an array with fields that directly match to the incoming array. I’m not sure if there is a better way to structure my data, or if I’m just not experienced enough in aggregations

Incoming document from $lookup

{
 _id: ObjectId(),
 name: "some name",
 wears: [
    {
     _id: ObjectId(),
     price: 66.54
    }
 ]
}

Existing document in pipeline:

{
_id: ObjectId(),
name: "Name",
price: 1000,
items: [
    {
     _id: ObjectId, // reference to  the other root level document in the other collection
     odds: 0.25,
     wears: [
     {
      _id: ObjectId(),
      odds: 0.5,
      rollMin: 0,
      rollMax: 25000
     }
     ]
    }
]
} 

I attempted to do this by unwinding the arrays and merging the objects, but it only merged the root level data, not the nested “items” array.

The reason for having to link the data like this, is if I need to update the price of an item, I want those changes reflected easily in the documents that are linking to it, without having to write a way to keep the prices in-sync like if they were embedded.

Happy to answer any answers, or give more information if needed.

Could you provide the pipeline you used? I’m not sure I understand what you mean by this exactly. I suspect there’s been discussion of doing what you may be describing but I don’t want to point you to it if it doesn’t match what you’re trying to accomplish.

Asya

Here is a rough example of an aggregation I did.

[{$lookup: {
 from: 'skins',
 localField: 'items._id',
 foreignField: '_id',
 as: 'merged'
}}, {$unwind: {
 path: '$items'
}}, {$unwind: {
 path: '$merged'
}}, {$project: {
 test: {
  $mergeObjects: [
   '$merged',
   '$items'
  ]
 }
}}]

This merges the root level items in the object, but not the content in the “wears” array.

I was able to get the desired result with this. I am open to suggestions to improving this however.

{
    '$set': {
      'items': {
        '$map': {
          'input': '$incoming', 
          'in': {
            '$let': {
              'vars': {
                'item': {
                  '$arrayElemAt': [
                    '$items', {
                      '$indexOfArray': [
                        '$incoming._id', '$$this._id'
                      ]
                    }
                  ]
                }
              }, 
              'in': {
                '$mergeObjects': [
                  '$$this', {
                    'odds': '$$item.odds', 
                    'wears': {
                      '$map': {
                        'input': {
                          '$arrayElemAt': [
                            '$items.wears', {
                              '$indexOfArray': [
                                '$items._id', '$$this._id'
                              ]
                            }
                          ]
                        }, 
                        'as': 'wear', 
                        'in': {
                          '$mergeObjects': [
                            '$$wear', {
                              '$arrayElemAt': [
                                '$$this.wears', {
                                  '$indexOfArray': [
                                    '$items.wears._id', '$$this.wears._id'
                                  ]
                                }
                              ]
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    }
  }