I am looking to merge nested arrays using $reduce and $setUnion in MongoDB. Following is the sample input -
levels: [
[[80,100,120]],[[100,150]],[[200,80,10
You can use below aggregation with single $project stage
db.collection.aggregate([
{ "$project": {
"levels": {
"$reduce": {
"input": {
"$reduce": {
"input": "$levels",
"initialValue": [],
"in": { "$setUnion": ["$$this", "$$value"] }
}
},
"initialValue": [],
"in": { "$setUnion": ["$$this", "$$value"] }
}
}
}}
])