Try using the unit:
db.people.aggregate([ {$unwind:"$colors"}, {$group:{_id:null, clrs: {$push : "$colors"} }}, {$project:{_id:0, colors: "$clrs"}} ])
Result:
{ "result" : [ { "colors" : [ "blue", "red", "blue", "purple", "pink" ] } ], "ok" : 1 }
Update
If you want to get unique values ββin an array of results, you can use $ addToSet instead of $push at the $group stage.
Yevgeniy Anfilofyev
source share