How to get a distinct value of a row with sequelize? How to get a distinct value of a row with sequelize? express express

How to get a distinct value of a row with sequelize?


You can specify distinct for one or more attributes using Sequelize.fn

Project.findAll({    attributes: [        // specify an array where the first element is the SQL function and the second is the alias        [Sequelize.fn('DISTINCT', Sequelize.col('country')) ,'country'],        // specify any additional columns, e.g. country_code        // 'country_code'    ]}).then(function(country) {  })


Try with this: https://github.com/sequelize/sequelize/issues/2996#issuecomment-141424712

Project.aggregate('country', 'DISTINCT', { plain: false }).then(...)


I ended up using grouping like this:

Project.findAll({  attributes: ['country'],  group: ['country']}).then(projects =>   projects.map(project => project.country));

It results into distinct models you can nicely iterate.

Link in previous answer helped little bit: https://github.com/sequelize/sequelize/issues/2996#issuecomment-141424712

This works well too, but generating response with DISTINCT as column name:

Project.aggregate('country', 'DISTINCT', { plain: false })  .then(...)