UPDATE: After some trial and error, it seems like using the find method won't work for this particular scenario. I managed to come up with a workaround by introducing a boolean field called "last_inserted" and utilizing Meteor hooks to ensure that only the last inserted record has this field. Thank you to everyone for your input.
Currently, I'm working on a Meteor project that involves a collection structured like this:
group_id, date, attribute1, attributeN
1, 2015-11-26 09:40:23.000Z, "foo", "bar"
1, 2015-11-23 14:23:53.000Z, "foo", "bar"
2, 2015-11-23 14:24:01.000Z, "foo", "bar"
2, 2015-11-23 14:25:44.000Z, "foo", "bar"
I need to filter this collection to retrieve, for each group_id, the record with the latest date, resulting in something like this:
group_id, date, attribute1, attributeN
1, 2015-11-26 09:40:23.000Z, "foo", "bar"
2, 2015-11-23 14:25:44.000Z, "foo", "bar"
I've explored using the aggregate
operator as suggested in a similar question on Stack Overflow:
MongoDB - get documents with max attribute per group in a collection
However, Meteor currently does not support this operation, so I need to achieve the desired result using a find
query.
While I'm not a MongoDB expert, I've been extensively going through the documentation but I'm beginning to think that achieving this without fetching all records and filtering them in JavaScript might not be possible (which is a less than ideal solution for me)
I've come across some Meteor packages that claim to add the aggregate command (like this), but I'm unsure of their reliability since the project I'm working on requires a robust solution.
Any insights on how I can accomplish this task?