My document structure in CouchDB looks like below:
{
"_id": "0a68cdbe4a7f3abf4046bc",
"_rev": "1-1508",
"score": {"math":90, "physics": 88, "chemistry": 60},
"student_name": "Mike"
}
I need to show below stats in the front end to setup a student’s profile:
- Given a student _id, how can I retrieve that student’s rank in each course;
- Given a student _id, how can I retrieve that student’s rank for his total score (math+physics+chemistry).
Suppose I have only 2 students, the 2nd student’s record is like below:
{
"_id": "0a68cdbe2344a3abf4046bc",
"_rev": "1-1608",
"score": {"math":80, "physics": 98, "chemistry": 90},
"student_name": "Jane"
}
So Mike’s rank should be:
math: 1
physics: 2
chemistry: 2
total: 2
and Jane’s rank should be
math: 2
physics: 1
chemistry: 1
total: 1
Let me know if I did not state the problem clearly.
I did not figure out the way to create views to get the rank. What I have tried:
- create views that map score to student info. Then I can query a score range to get students that their score is in that range.
Edit: the functionality of query by user name and retrieval of rank does not need to be implemented by a view only. Any idea is welcomed!
Edit2: The number of courses would be 1K to 3K. The number of students would be 1M to 2M.
I have an idea that’s based on @joscas’s answer.
You can create a view like this:
I made the assumption that the score range for each course is from 0 to 100. The idea is that:
bucket0tobucket100).Given a course name
cand your scores, this second view tells youhow many students play not as good as you in this course, from which you can derive the rank by usingn-#s, wherenis the total student number enrolled inc,#sis the number of students who get lower score thans. For instance, a query of"math", 99will return 32374-32324 = 50 which is the rank of the student who got 99 in “math”.For the total score part of your questions, you can use the similar idea but change the bucket size and number.