I am in the middle of complex aggregation query (with lookup and many group/unwind stages) and encounter problem with combining two different arrays from different fields.
As for now, after one of the stages (in the middle or after lookup of my big query) I have the following output:
{
_id: 1,
quantity: [2, 1, 3]
reagents: [
{name: "FirstItem", other_field: ... },
{name: "SecondItem", other_field: ... },
{name: "ThirdItem", other_field: ... }
]
}
And I want:
_id: 1,
reagents: [
{name: "FirstItem", quantity: 2 },
{name: "SecondItem", quantity: 1 },
{name: "ThirdItem", quantity: 3 }
]
So each [quantity] value I want to add to every object inside [reagents] as field according to it's index, like first {object} inside reagents should have first element from [quantity]
Schema with array indexes):
quantity: [2, 1, 3, ... n]
↓ ↓ ↓ ↓
reagents: [1, 2, 3, ... n]
THE PROBLEM:
I cannot do it after or before aggregation because:
I don't have a necessary data, cause it's result of lookup from different collections.
I cannot do it after because right after this, I should
$groupdata by another field, so[reagents]sorting order have been lost and cannot be restored.
UPD: I understand that I could (and probably should use $unwind stage for [reagents] but I haven't found any relevant Aggregation Stage Operators in Mongo Manual. So if you know a necessary one, please point me in.