MongoDB - Aggregation - To get unique items in array MongoDB - Aggregation - To get unique items in array mongodb mongodb

MongoDB - Aggregation - To get unique items in array


After few more tries, I had solved this. Here's the commands:

db.xyz.aggregate( {$project: {a: '$products.item'}},     {$unwind: '$a'},     {$unwind: '$a'},     {$group: {_id: 'a', items: {$addToSet: '$a'}}});

and

db.xyz.aggregate( {$project: {category: 1, a: '$products.item'}},     {$unwind: '$a'},     {$unwind: '$a'},     {$group: {_id: '$category', items: {$addToSet: '$a'}}});


After mongodb3.4, there is a $reduce operator, so we can flat a array without extra stage.

1.

col.aggregate([  {    $project: {      items: {        $reduce: {          input: "$products.items",          initialValue: [],          in: { $concatArrays: ["$$value", "$$this"] },        },      },    },  },  { $unwind: "$items" },  { $group: { _id: null, items: { $addToSet: "$items" } } },]);

2.

col.aggregate([  {    $project: {      category: 1,      items: {        $setUnion: {          $reduce: {            input: "$products.items",            initialValue: [],            in: { $concatArrays: ["$$value", "$$this"] },          },        },      },    },  },]);


I know it is an old question and you've solved it several years ago! But there is a small problem in the answer you've marked as correct and it may not suitable for all cases. The $unwind is an expensive operator and may affect latency and memory consumption for large datasets. I think the $reduce operator is more performant in this case.