MongoDB Compass: select distinct field values MongoDB Compass: select distinct field values mongodb mongodb

MongoDB Compass: select distinct field values


You can do this via aggregation framework in Compass, using $unwind and $group. The $unwind is performed to create a unique document for each element in the target array, which enables the $addToSet operator in the $group stage to then capture the genres as distinct elements.

Pipeline:

[  {    $unwind: {      path: '$genre',      preserveNullAndEmptyArrays: true    }  },  {    $group: {      _id: null,      uniqueGenres: { $addToSet: '$genre' }    }  }]

See screenshot below for Compass example:

enter image description here