Mongodb count the number of consecutive matching docs and merge them into one

Hey thanks for the response and suggestion. The IDs were actually meant to all be unique by changing one letter for each one, I should’ve used simpler IDs. And now strangely it seems I can’t edit the post. Anyways I want to identify groups based on if the groupedMsgs follow each other when they’re sorted by time (i.e. the date property). So the second and third messages would be a group containing 2 items because they come after each other, but the fifth would be another group of 1. I haven’t found a pipeline using $group that achieves this, once I add the type as a group ID then all the groupedMsgs get put into that group.