Mak*_*mat 7 postgresql charts node.js sequelize.js c3.js
我想创建一个查询,它能够在sequelize.js中一次计算每月每天的记录数量.不喜欢:
Record.count({ where: { createdAt: { $like: '2015-04-14%' } } }).then(function(c) {
console.log("2015-04-14 have been created" + c + "records");
});
Record.count({ where: { createdAt: { $like: '2015-04-15%' } } }).then(function(c) {
console.log("2015-04-15 have been created" + c + "records");
});
Record.count({ where: { createdAt: { $like: '2015-04-16%' } } }).then(function(c) {
console.log("2015-04-16 have been created" + c + "records");
});
....
....
Run Code Online (Sandbox Code Playgroud)
我想进行一次性返回行数的查询,而不是在30个查询中询问数据库中的数据.有可能通过交易来实现吗?
我将它用于图表目的,所以最好的输出就像:
[500, 300, 400, 550....]
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助!
对于这种类型的查询,您可以使用postgresql date_trunc函数进行分组:
db.record.findAll({
attributes: [
[
db.sequelize.fn('date_trunc',
'day',
db.sequelize.col('createdAt')
),
'dateTrunc'
],
[
db.sequelize.fn('count',
db.sequelize.col('id')
),
'count'
]
],
group: '"dateTrunc"'
}).then(function(rows) {
console.log(rows);
});
Run Code Online (Sandbox Code Playgroud)