Javascript 续集 findOne 最新条目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35445849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:44:07 来源:igfitidea点击:
Sequelize findOne latest entry
提问by Noah
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.
我需要在表格中找到最新的条目。做这个的最好方式是什么?该表具有 Sequelize 的默认createdAt
字段。
回答by Krzysztof Sztompka
Method findOne
is wrapper for findAll
method. Therefore you can simply use findAll
with limit 1 and order by id descending.
方法findOne
是方法的包装器findAll
。因此,您可以简单地使用findAll
limit 1 并按 id 降序排序。
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]
});
回答by szym
model.findOne({
where: {
key: key,
},
order: [ [ 'createdAt', 'DESC' ]],
});