Sequelize findOne latest entry

主宰稳场 提交于 2019-12-20 08:46:45

问题


I need to find the latest entry in a table. What is the best way to do this? The table has Sequelize's default createdAt field.


回答1:


Method findOne is wrapper for findAll method. Therefore you can simply use findAll with limit 1 and order by id descending.

Example:

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]
}); 



回答2:


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


来源:https://stackoverflow.com/questions/35445849/sequelize-findone-latest-entry

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!