续集findOne最新条目
我需要在表格中find最新的条目。 做这个的最好方式是什么? 该表具有Sequelize的默认createdAt字段。
方法findOne
是findAll
方法的封装。 因此,您可以简单地使用限制1的findAll
,并按id降序排列。
例:
YourModel.findAll({ limit: 1, where: { //your where conditions, or without them if you need ANY entry }, order: [ [ 'createdAt', 'DESC' ]] }).then(function(entries){ //only difference is that you get users list limited to 1 //entries[0] });
model.findOne({ where: { key: key, }, order: [ [ 'createdAt', 'DESC' ]], });