You were on the right track with $reduce. The enclosing $expr and $gt were not. The $add was not the correct operator. Try:
{ "$set" : {
"barcodes" :
{
"$reduce" :
{
"input" : "$barcodes" ,
"initialValue" : [] ,
"in" : { "$concatArrays" : [
"$$value' ,
{ "$cond" : [
{ "$eq" : [ {"$type" : "$$this"} , "array" ] } ,
"$$this" ,
[ "$$this" ]
] }
] }
}
}
} }
In migration scenarios like yours, what I like to do is to migrate $out into a temporary collection, which I can verify before $merge-ing it back to the real collection. But you need downtime because between the $out and $merge, original data may change.