node.jssails.jswaterline

get user's rank by points and registration date in sails.js


How to get the users rank using it's points and registration time ? i have tried to get users by same criteria and it's working fine.

  await User.find({
      sort: {
          rewardPoints: 0,
          createdAt: 1
      }   
  }).limit(limit);

Now i want to get my rank only but it is not giving me correct result's by this code

 await User.count({ 
     createdAt: { "<" : user.createdAt },
     rewardPoints: { ">=": user.rewardPoints },
  });

Solution

  • i was finally did it using a sql query:

    SELECT id
              , firstName
              , rank
              FROM ( 
                SELECT 
                *
                , (@rank := @rank + 1) AS rank
                FROM 
                user
                CROSS JOIN( 
                SELECT
                  @rank := 0
                )  
                AS 
                  init_var_var
                ORDER BY
                user.rewardPoints DESC,
                user.createdAt ASC    
                )
              AS logins_ordered_ranked
              WHERE
                id = "${user.id}"