[英]Sequelize findOne latest entry
我需要在表格中找到最新的條目。 做這個的最好方式是什么? 該表有Sequelize的默認createdAt
場。
model.findOne({
where: { key },
order: [ [ 'createdAt', 'DESC' ]],
});
方法findOne
是findAll
方法的包裝器。 因此,您可以簡單地使用findAll
限制為 1 並按 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]
});
如果有人將時間戳設為假,您也可以使用 id 來實現。 這個對我有用。
model.findOne({
order: [ [ 'id', 'DESC' ]],
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.