Having a documents collection ordered by field, I want to group documents by other field but only adjacent documents should be grouped. Like so:
[
{order: 1, state: 'one'},
{order: 2, state: 'one'},
{order: 3, state: 'one'},
{order: 4, state: 'two'},
{order: 5, state: 'two'},
{order: 6, state: 'one'},
{order: 7, state: 'two'},
{order: 8, state: 'three'},
{order: 9, state: 'three'}
]
should result in:
[
[
{order: 1, state: 'one'},
{order: 2, state: 'one'},
{order: 3, state: 'one'}
],
[
{order: 4, state: 'two'},
{order: 5, state: 'two'}
],
[
{order: 6, state: 'one'}
],
[
{order: 7, state: 'two'}
],
[
{order: 8, state: 'three'},
{order: 9, state: 'three'}
]
]
Unfortunately I’m quite new to MongoDb so I have little to no idea even where to start from.
I suspect this can be achieved with some king of aggregation, but have no idea where to start from.
Tried $group with _id: null and $setWindowFields and bucketing, but with no success.