Kenzo
Kenzo

Reputation: 366

How do I get the top X number of users with the most Y in Sequelize ORM?

I want to get the top 10 users with the highest follower count. How do I do this using the Sequelize ORM? Seems as though you would be using SELECT TOP number|percent column_name(s) with vanilla SQL, but can't seem to find the same func with Sequelize.

Upvotes: 5

Views: 9809

Answers (3)

Megrax
Megrax

Reputation: 123

For v6, you may refer to the doc

User.findAll({ 
    limit: 10 ,
    order: [['follower', 'DESC']]
})

Upvotes: 1

KenOn10
KenOn10

Reputation: 1968

SELECT TOP doesn't work in every database vendor (maybe only Microsoft SQL Server?). The sequelize doc describes "LIMIT" (used by MySql), which might work for your database. Here's an example:

/*Find the 5 most relevant answers:*/
Answers.findAll({
  order: [[Sequelize.col(relevance_index),'DESC']],
  limit: 5
  })        

Upvotes: 1

Chuong Tran
Chuong Tran

Reputation: 3441

Base on a document from Sequelize about order & limit, we have a simple code

User.findAll({ 
    limit: 10 ,
    order: 'follower DESC'
})

Upvotes: 9

Related Questions