Aggregating and searching data from only the latest records of an identifier

{'name': 'student1', 'date': '2019-03-12', 'grade': 3}
{'name': 'student2', 'date': '2019-03-12', 'grade': 4}
{'name': 'student3', 'date': '2019-03-12', 'grade': 5}
{'name': 'student4', 'date': '2019-03-12', 'grade': 4}
{'name': 'student1', 'date': '2019-09-01', 'grade': 2}
{'name': 'student2', 'date': '2019-09-01', 'grade': 3}
{'name': 'student3', 'date': '2019-09-01', 'grade': 4}

Consider the above as my sample dataset wherein I'm able to query the data and generate list of students along with the latest grade, like below:

{'name': 'student1', 'date': '2019-09-01', 'grade': 2}
{'name': 'student2', 'date': '2019-09-01', 'grade': 3}
{'name': 'student3', 'date': '2019-09-01', 'grade': 4}
{'name': 'student4', 'date': '2019-03-12', 'grade': 4}

Issue arises, when I make a query to see the list of students who have grade 4, it gives me result as:

{'name': 'student2', 'date': '2019-03-12', 'grade': 4}
{'name': 'student3', 'date': '2019-09-01', 'grade': 4}
{'name': 'student4', 'date': '2019-03-12', 'grade': 4}

While I need to query only the latest record of students and get only two records since as per latest records, grade of student2 is 3 and total response expected is as below:

{'name': 'student3', 'date': '2019-09-01', 'grade': 4}
{'name': 'student4', 'date': '2019-03-12', 'grade': 4}

How can I achieve this result, so that I'm searching for grade 4 students considering the fact that only their latest record is being searched.

One way I can think of is to write a script which will do a query to get the document id of latest record of a particular student and the query will use time range to get only those records which got changed recently. Then take values of updated fields from latest document and insert them into a different index. This can run periodically and would only keep latest record of each student in second index.

I can later get my Dashboards and queries being done from this second index now, to get my desired results.

Tried to think about other solutions, but couldn't think of any other way round.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.