Using the aggregation framework I want to add the index of each element inside the array to the subdocument itself.
In a later stage I'd like to $unwind
the elements, but then I need the old index inside the myResults Array.
Current structure:
{ myResults : [
{ foo: "bar"
},
{ answer: 42
}
]
Target structure:
{ myResults : [
{ index: 0, // <-- this should be added
foo: "bar"
},
{ index: 1, // <-- this should be added
answer: 42
}
]
Note: I don't want to update the elements, - I just want to use the index in a later stage of the aggregation pipeline.