Mongoose: aggregate query to return an array of strings instead to array of objects

Viewed 102

I want to make the same operation like this to return an array of strings:

model.find().distinct("some_field")

How it's possible to take the same result using aggregation function?

1 Answers

You can use below code to get all unique some_field using aggregate:

model.aggregate([
    {
        $group:{
           _id: null,
           some_field: { $addToSet: '$some_field' }
        }
    }
]);
Related