How i can do mongodb group by query with like sql over by PARTITION ?
{
"id": "...",
"section": "x"
"items": [
{
"id": 1,
"count": 10,
}, {
"id": 2,
"count": 20,
}
]
},
{
"id": "...",
"section": "x"
"items": [
{
"id": 1,
"count": 100,
}, {
"id": 2,
"count": 200,
}
]
}
db.c.aggregate([
{ $unwind: "$items"},
{ $group :
{
_id : "$items.id",
SumCount: { $sum: "$items.count" },
}
}
]);
Now result:
1 | 110
2 | 220
But need add column count by field ‘section’ like in sql :
COUNT(section) OVER (PARTITION BY section) AS [COUNT_Section]
Need result:
1 | 110 | 2 -- two document with section:'x'
2 | 220 | 2