Issue
I am looking to generate two arrays of equal length without any null
elements.
Solution
I have managed to create two arrays, but they contain some null
values. When I remove the null
values, the arrays are no longer of equal length.
aggregate([
{
$unwind: '$row'
}, {
$match: {
$or: [{
'row.identifier': 'fah'
}, {
'row.identifier': 'agr'
}]
}
}, {
$group: {
_id: '$row.identifier',
rows: {
$push: '$row.value'
}
}
}
]
Current Output
[[5, null, 64, 34, 1], [53, 31, null, null, 7]]
There are still null
values present in the arrays.
Desired Output:
[[5, 1], [53, 7]]
The goal is to remove null
values and eliminate values at the same index.
Update 1
Provided below are examples of two documents as requested:
[{ // 1st
row: [{
value: 53,
identifier: 'agj'
}, {
value: 51,
identifier: 'hrw'
}, {
value: null,
identifier: 'rgs'
}]
}, { // 2nd
row: [{
value: null,
identifier: 'agj'
}, {
value: 72,
identifier: 'hrw'
}, {
value: 11,
identifier: 'rgs'
}]
}]