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.