Imagine the following sample data:
[
{
"_id": 1,
"category": "FIRE",
"time": "2024-05-11T07:11:00Z"
},
{
"_id": 2,
"category": "FIRE",
"time": "2024-05-11T08:11:00Z"
},
{
"_id": 3,
"category": "FIRE",
"time": "2024-05-11T09:11:00Z"
},
{
"_id": 4,
"category": "POLICE",
"time": "2024-05-11T07:22:00Z"
},
{
"_id": 5,
"category": "POLICE",
"time": "2024-05-11T08:22:00Z"
},
{
"_id": 6,
"category": "POLICE",
"time": "2024-05-11T09:22:00Z"
},
{
"_id": 7,
"category": "AMBULANCE",
"time": "2024-05-11T07:33:00Z"
},
{
"_id": 8,
"category": "AMBULANCE",
"time": "2024-05-11T08:33:00Z"
},
{
"_id": 9,
"category": "AMBULANCE",
"time": "2024-05-11T09:33:00Z"
}
]
For each combination in a set of categories (e.g. ["FIRE", "AMBULANCE"]
) and timestamps (e.g. ["2024-05-11T08:15:00Z", "2024-05-11T09:00:00Z"]
), I’d like to get the latest entry on or before each timestamp for each category:
[
{
"category": "FIRE",
"time": "2024-05-11T08:15:00Z",
"last_entry_on_or_before": {
"_id": 2,
"category": "FIRE",
"time": "2024-05-11T08:11:00Z"
}
},
{
"category": "FIRE",
"time": "2024-05-11T09:00:00Z",
"last_entry_on_or_before": {
"_id": 2,
"category": "FIRE",
"time": "2024-05-11T08:11:00Z"
}
},
{
"category": "AMBULANCE",
"time": "2024-05-11T08:15:00Z",
"last_entry_on_or_before": {
"_id": 7,
"category": "AMBULANCE",
"time": "2024-05-11T07:33:00Z"
}
},
{
"category": "AMBULANCE",
"time": "2024-05-11T09:00:00Z",
"last_entry_on_or_before": {
"_id": 8,
"category": "AMBULANCE",
"time": "2024-05-11T08:33:00Z"
}
}
]
Is this possible to do in a single efficient aggregation pipeline? I have a compound index on [category, time]
.