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

You got me right @Pavel_Duchovny

it’s just that @Uchechukwu_Ozoemena will have to maintain it because the bucket definition depends on the sorted messages list.

so if we are really talking about chat messages I assume these buckets wouldn’t have to be changed over time and it will be fine, so if it is not the case you should reconsider the bucket pattern.

in a new message insertion of type ‘groupedMsgs’ you should check if the lastest document is ‘groupedMsgs’ bucket if it is - push the new element and increase numConsecutiveItems, otherwise create a new bucket

after that, the find documents should be very straightforward

1 Like