续集 findOne 最新条目 [英] Sequelize findOne latest entry

查看:42
本文介绍了续集 findOne 最新条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在表格中找到最新的条目.做这个的最好方式是什么?该表具有 Sequelize 的默认 createdAt 字段.

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.

推荐答案

Method findOnefindAll 方法的包装器.因此,您可以简单地使用 findAll 限制为 1 并按 id 降序排序.

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

示例:

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

这篇关于续集 findOne 最新条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆