Sequelize grouping by date, disregarding hours/minutes/seconds
For Sequelize and MYSQL
This what worked for Me:
Model.findAll({
attributes: [
/* add other attributes you may need from your table */
[sequelize.fn('DATE', sequelize.col('createdAt')), 'Date']
],
group: [sequelize.fn('DATE', sequelize.col('createdAt')), 'Date']
})
As you said, it's done with sequelize.fn(...)
and there is no other way. Try:
Task.findAll({
group: [sequelize.fn('date_trunc', 'day', sequelize.col('createdAt'))]
})
I think that might do the job. If not, we'll see how to do it ;)
Notice that PostgreSQL allows you to truncate to specific intervals. For more information visit: http://www.postgresql.org/docs/9.1/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC
Also, to understand how group (and order) works see the documentation of Sequelize: https://github.com/sequelize/sequelize/blob/172272c8be9a847b2d64f0158826738703befddf/docs/docs/models-usage.md#manipulating-the-dataset-with-limit-offset-order-and-group
The selected answer didn't work here.
This is what is working for me.
Task.findAll({
attributes: [
[Sequelize.literal(`DATE("createdAt")`), 'date'],
[Sequelize.literal(`COUNT(*)`), 'count']
],
group: ['date'],
})