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

Hi @Uchechukwu_Ozoemena,

The presented dataset has all the _id’s with the same value. So I am not sure How do you identify messages into groups? Is it just by the fact that they have groupedMsgs and they are by date the following object?

Can you explain the application side idea of getting this data set? Wouldn’t it be better to group those messages as an embeeded array inside a parent doc?

{
    _id: '18c9bb49-03f4-4dc4-b0ba-a3c45c324347',
    chatID: 'alternateChat',
    type: 'txt',
    message: 'first message',
    senderID: 'first_sender_id',
    date: new Date('0001-01-02T01:01:11.001Z')
  },
  {
    _id: '18c9bb4w-03f4-4dc4-b0ba-a3c45c324347',
    chatID: 'alternateChat',
    type: 'groupedMsgs',
    message: 'second message',
    senderID: 'first_sender_id',
    date: new Date('0001-01-03T01:01:11.001Z'),
    ConsecutiveItems: [{
    _id: '18c9bb4w-03g4-4dc4-b0ba-a3c45c324347',
    chatID: 'alternateChat',
    type: 'groupedMsgs',
    message: 'third message',
    senderID: 'first_sender_id',
    date: new Date('0001-01-04T01:01:11.001Z')
  }
  }
...

Pavel

1 Like