As a beginner in the world of MongoDB, I'm trying to figure out how to extract data from an array in separate sections.
{
"name":"User name",
"messages":[
{
"message":"Message 1",
"date":"2022-05-12T00:00:00.000Z"
},
{
"message":"Message 2",
"date":"2022-05-12T00:00:00.000Z"
},
{
"message":"Message 3",
"date":"2022-05-13T00:00:00.000Z"
},
{
"message":"Message 4",
"date":"2022-05-13T00:00:00.000Z"
},
{
"message":"Message 5",
"date":"2022-05-13T00:00:00.000Z"
}
]
}
The desired outcome that I'm aiming for is
[
{
"date":"2022-05-12T00:00:00.000Z",
"messages":[
{
"message":"Message 1",
"date":"2022-05-12T00:00:00.000Z"
},
{
"message":"Message 2",
"date":"2022-05-12T00:00:00.000Z"
}
]
},
{
"date":"2022-05-13T00:00:00.000Z",
"messages":[
{
"message":"Message 3",
"date":"2022-05-13T00:00:00.000Z"
},
{
"message":"Message 4",
"date":"2022-05-13T00:00:00.000Z"
},
{
"message":"Message 5",
"date":"2022-05-13T00:00:00.000Z"
}
]
}
]
I haven't had much exposure to MongoDB aggregation yet and while I've searched through numerous queries on stack overflow, none have provided me with the assistance I need.
Is there a way to achieve this? If so, I would greatly appreciate any guidance. :)