Sort sequelize.js query by date

天大地大妈咪最大 提交于 2020-08-22 07:14:40

问题


Post
  .findAll({where: {tag: 'news'}, limit: 10})
  .success(function(result) { ... })

How to insert the condition of sorting by date in my query with not using sequelize.query like

.findAll({ limit: 10, sort: [updatedAt, descending]})

回答1:


Here's the syntax:

Post.findAll({ limit: 10, order: '"updatedAt" DESC' })

Here are more examples from the official documentation.




回答2:


@dankohn is correct and that will work but it is safer to use the following:

Post.findAll({ limit: 10, order: [['updatedAt', 'DESC']]});

as this

will escape updatedAt and validate DESC against a list of valid direction parameters




回答3:


Following is more cleaner syntax

Post.findAll({ limit: 10, order: 'updatedAt DESC'});



来源:https://stackoverflow.com/questions/20718534/sort-sequelize-js-query-by-date

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