im new to mongodb and Im having some trouble to group an array from a document like this:
_id: ObjectId('5d7afa2609d6ed000dffe1de')
email: "[email protected]"
transactions: [
{
"date": "2020-06-10T00:00:00.000+00:00",
"shares": 100,
"price": 20,
"type": "buy",
"equity_id": "petr4"
},
{
"date": "2020-07-10T00:00:00.000+00:00",
"shares": 200,
"price": 10,
"type": "sell",
"equity_id": "petr4"
},
{
"date": "2020-06-10T00:00:00.000+00:00",
"shares": 250,
"price": 30,
"type": "buy",
"equity_id": "vale3"
}, ...
]
I would like to group these transactions by date and obtain an document like this:
_id: ObjectId('5d7afa2609d6ed000dffe1de')
email: "[email protected]"
transactionsByDay: {
"2020-06-10": [
{
"shares": 100,
"price": 20,
"type": "buy",
"equity_id": "petr4"
},
{
"shares": 250,
"price": 30,
"type": "buy",
"equity_id": "petr4"
}
],
"2020-07-10": [
{
"shares": 200,
"price": 10,
"type": "sell",
"equity_id": "petr4"
}
], ...
}
I tried an aggregation using a group operator but the result didn’t came as expected. Can someone give me a help to solve this?