我的架构如下:
测试
const test = sequelize.define('test', {
id: {primaryKey: true, type: DataTypes.INTEGER, autoIncrement: true, allowNull: false, unique: true},
start_date: {type: DataTypes.DATE},
});
return test
现在在不同的行中,我将start_date设为2018-05-05,2018-05-06 and 2018-06-06.
现在我想要的是,我想获得结果2018-05-05 and 2018-05-06
,这是当前月份和年份。我应该如何在sequelize中提出查询?
答案 0 :(得分:2)
如果您询问如何选择日期,那么只要您始终使用提供的JavaScript Date
对象和列的DATE
类型工作,那么简单的范围选择只是一个问题提供日期并使用范围运算符,例如$gte
和$lte
const { Op } = Sequelize = require('sequelize');
let query = {
where: {
start_date: {
[Op.gte]: new Date("2018-03-01"),
[Op.lt]: new Date("2018-04-01")
}
}
};
let data = await DateTest.findAll(query);
当然,首选的Symbol
实现需要一个现代的nodejs环境来支持对象的表示法。如果你没有,那么你仍然可以使用"字符串":
let query = {
where: {
start_date: {
'$gte': new Date("2018-03-01"),
'$lt': new Date("2018-04-01")
}
}
};
如果您希望对GROUP BY
类型的列DATE
进行const { Op } = Sequelize = require('sequelize');
const log = data => console.log(JSON.stringify(data, undefined, 2));
const sequelize = new Sequelize('sqlite:dates.db', { logging: log });
const DateTest = sequelize.define('DateTest', {
start_date: Sequelize.DATE
});
(async function() {
try {
await sequelize.authenticate();
await DateTest.sync({ force: true });
let result = await sequelize.transaction(transaction =>
Promise.all(
['2018-03-01','2018-03-01T01:12:23.000Z','2018-04-01'].map(d =>
DateTest.create({ start_date: new Date(d) },{ transaction })
)
)
);
let query = {
attributes: [
[
sequelize.fn('strftime', '%Y-%m-%d', sequelize.col('start_date')),
'date'
],
[sequelize.fn('count','*'),'count']
],
group: [sequelize.col('date')],
where: {
start_date: {
[Op.gte]: new Date("2018-03-01"),
[Op.lt]: new Date("2018-04-01")
}
}
};
console.log(query);
let data = await DateTest.findAll(query);
log(data);
} catch(e) {
console.error(e)
} finally {
process.exit()
}
})()
,那么这些方法实际上会因后端的数据库引擎而异,因此最好使用完整列表进行演示:
const { Op } = Sequelize = require('sequelize');
const uri = 'postgres://user:password@localhost/database';
const log = data => console.log(JSON.stringify(data, undefined, 2));
const sequelize = new Sequelize(uri, { logging: log });
const DateTest = sequelize.define('DateTest', {
start_date: Sequelize.DATE
});
(async function() {
try {
await sequelize.authenticate();
await DateTest.sync({ force: true });
let result = await sequelize.transaction(transaction =>
Promise.all(
['2018-03-01', '2018-03-01T01:12:23.000Z','2018-04-01'].map(d =>
DateTest.create({ start_date: new Date(d) },{ transaction })
)
)
);
let query = {
attributes: [
[
sequelize.fn('date_trunc', 'day', sequelize.col('start_date')),
'date'
],
[sequelize.fn('count','*'),'count']
],
group: [
sequelize.col('date')
],
where: {
start_date: {
[Op.gte]: new Date("2018-03-01"),
[Op.lt]: new Date("2018-04-01")
}
}
};
console.log(query);
let data = await DateTest.findAll(query);
log(data);
} catch(e) {
console.error(e)
} finally {
process.exit()
}
})()
date_trunc()
实际上[
{
"date": "2018-03-01",
"count": 2
}
]
应该适用于各种SQL RDBMS后端,但特别是不能使用SQLite,以防万一你甚至考虑用于测试目的,如果不是" lite"分布。
两种形式都会输出:
GROUP BY
表示我们只选择了#34; 3月份的日期发生在带有查询条件的插入样本中,而2
则截断日期为" day"为了达到指定日期的body is missing data
总预期数。