本文介绍了续集 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 findOne 是 findAll 方法的包装器.因此,您可以简单地使用 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 最新条目
发布评论