I'd like to get two equally long arrays where both don't contain null
elements.
I can successfully return two arrays, but they contain null
values. When I exclude null
values they're of course not equally long.
aggregate([
{
$unwind: '$row'
}, {
$match: {
$or: [{
'row.identifier': 'fah'
}, {
'row.identifier': 'agr'
}]
}
}, {
$group: {
_id: '$row.identifier',
rows: {
$push: '$row.value'
}
}
}
]
[[5, null, 64, 34, 1], [53, 31, null, null, 7]]
null
values still present.
[[5, 1], [53, 7]]
null
values and values at the same index are removed.
Here are two example 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'
}]
}]
There may be a way of doing this filtering in mongodb, I'm not that familiar with it. But in javascript you could write a filter function like the following:
var notNullFilter = function(inputs){
var outputs = [];
for (var i=0; i<inputs.length; i++){
outputs.push([]);
}
for (var k=0; k<inputs[0].length; k++){
var isNull = false;
for (var j=0; j<inputs.length; j++){
if (inputs[k][j] == null){
isNull = true;
break;
}
}
if (!isNull){
for (var l=0; l<inputs.length; l++){
outputs[l].push(inputs[l][k]);
}
}
}
};
If you were using a library like lodash this would be much easier but this function should work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.