Given a table with multiple records of different dates, something like
+------+------+---------------------+
| id | name | updated |
+------+------+---------------------+
| 100 | a | 2020-08-20 10:33:47 |
| 101 | b | 2020-08-20 10:34:05 |
| 102 | a | 2020-08-20 10:34:34 |
| 103 | b | 2020-08-20 10:34:34 |
| 104 | a | 2020-08-20 10:42:10 |
| 105 | a | 2020-08-20 10:42:36 |
+------+------+---------------------+
I want to retrieve the latest of each named record, in sql:
select t1.* from test t1 join (select name, max(updated) as updated from test group by name) t2 on t1.name = t2.name and t1.updated = t2.updated;
+------+------+---------------------+
| id | name | updated |
+------+------+---------------------+
| 103 | b | 2020-08-20 10:34:34 |
| 105 | a | 2020-08-20 10:42:36 |
+------+------+---------------------+
How do I do that in Mongodb?