簡體   English   中英

續集 findOne 最新條目

[英]Sequelize findOne latest entry

我需要在表格中找到最新的條目。 做這個的最好方式是什么? 該表有Sequelize的默認createdAt場。

model.findOne({
    where: { key },
    order: [ [ 'createdAt', 'DESC' ]],
});

方法findOnefindAll方法的包裝器。 因此,您可以簡單地使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM