Let's say that I have the following documents in a one of the stages in mongodb aggregation:
{
type: "A",
value: "A",
index: 1
},
{
type: "A",
value: "B",
index: 0
},
{
type: "B",
value: "a",
index: 4
},
{
type: "B"
value: "b",
index: 2
},
{
type: "B",
value: "c",
index: 5
}
And I would like to process them using available aggregate stages (using syntax from Mongo 4.0) into:
{
type: "A",
values: ["B", "A", null, null, null, null]
},
{
type: "B",
values: [null, null, "b", null, "a", "c"]
}
I was trying to use $project with $reduce but still I don't know how to set element at a specific index.
EDIT: The size of values array is not given up-front. In the sample case it's assumed to be 6 because the largest index is 5. So values in every output document has to be aligned to that size.
valuesarray?