0

Not sure how to approach this problem. Would greatly appreciate if some one can tell me if this is doable and if yes , how.

I have a collection in Mongo and every document in that collection has a field called Rank. When the user clicks a button, I have access to the req.user( with some custom fields) because I am using passport for authentication(both OAUTH and local).

Here is what I am looking for.

On the next page after the button click, I want to display 20 individuals who are ranked above the req.user rank. So assume I am the req.user and my rank is 24, I want all records having rank from 4 to 23 sorted from 4 to 23. Is this doable? Please guide.

This is how a single document in my collection looks like and the Rank field is somewhere in the middle

{
    "_id" : ObjectId("5ed3c6c2776524724f2fae63"),
    "Player_Name" : "Abhimanyu Sen",
    "Player_ID" : "P0001",
    "Player_ActivePlayer" : "N",
    "Edition_1_Rank" : "0",
    "Edition_2_Rank" : "0",
    "Edition_3_Rank" : "67",
    "Edition_4_Rank" : "113",
    "Edition_5_Rank" : "185",
    "Player_Owner" : "N",
    "Edition5_TeamOwner" : "N",
    "Edition6_TeamOwner" : "N",
    "Edition1_Performance" : "Did Not Play",
    "Edition2_Performance" : "Did Not Play",
    "Edition3_Performance" : "Round of 32 - Challengers Cup",
    "Edition4_Performance" : "Did Not Play",
    "Edition5_Performance" : "Did Not Play",
    "Total_Tournament_Matches_Played" : 8,
    "Total_Tournament_Matches_Won" : 0,
    "Total_Tournament_Matches_Lost" : 8,
    "Total_Tournament_Matches_Points" : 3.0,
    "Total_Challenge_Matches_Played" : 0,
    "Total_Challenge_Matches_Won" : 0,
    "Total_Challenge_Matches_Lost" : 0,
    "Total_Challenge_Matches_Points" : 0.0,
    "Total_Matches_Played" : 8,
    "Total_Matches_Won" : 0,
    "Total_Matches_Lost" : 8,
    "Total_Points" : 3.0,
    "Total_Games_Won" : 0,
    "Winning_Percentage" : 0.0,
    "Player_FDR" : 0.0,
    "Rank" : 185,
    "Player_ELORating" : "",
    "Player_IsHeavyTraveller" : "",
    "Player_NickName" : "",
    "Player_ImageURL" : "",
    "Player_CompanyName" : "",
    "Player_Designation" : "",
    "Player_DateOfBirth" : "",
    "Player_TelephoneNumber" : "",
    "Player_Website" : "",
    "Player_LinkedinProfile" : "",
    "Player_FBLink" : "",
    "Player_InstagramLink" : "",
    "Player_TwitterLink" : "",
    "Player_Interests" : "",
    "Player_Blog" : "",
    "Player_Posts" : "",
    "Player_Messages" : "",
    "Total_Games_Lost" : 24,
    "Total_Weighted_Points" : 2.65
}
3
  • please provide sample data json. Commented Jun 16, 2020 at 11:08
  • ana i posted above, but the formatting gets all skewed not sure what the issue is Commented Jun 16, 2020 at 11:11
  • Have you tried (sort:1) and then adding skip, limit to same query? Your question is somewhat ambiguous, is the first result showed to user not paginated (before clicking next)? Commented Jun 16, 2020 at 11:16

1 Answer 1

1

You can do as this:

 db.getCollection('your-collection').aggregate([
        {$match:{
            Rank: {$gt:24}
            }},
         {$sort:{Rank:1}},
         { $limit: 20
             }
        ])

Put the user's rank instead of 24

Sign up to request clarification or add additional context in comments.

2 Comments

ah got it that simple..sorry ana new to Mongo and wasting a lot of time navigating docs,,just easier asking a question..thanks once again you are the best
It's alright. I forgot the sort part. gonna fix it right now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.