Mongo DB distinct values group within array of objects Mongo DB distinct values group within array of objects mongodb mongodb

Mongo DB distinct values group within array of objects


name field should be inside group stage, that's why you have error.

Try this query:

db.getCollection('products').aggregate([    {        $unwind: '$features'    },    {        $unwind: '$features.values'    },    {        $unwind: '$features.values.values'    },    {        $group: {            _id: "$features.values.name",            values: {$addToSet: '$features.values.values'}        },    }])

Result:

{ "_id" : "feature2", "values" : [ "nest", "guest" ] }{ "_id" : "feature 1", "values" : [ "best", "test" ] }