'MongoDB - Group by inner element
I am explaining with a simple example see my MongoDB collection looks like this:
[
{
pid: erwer,
qty: 3,
LevelDetails: {
level1: { userId: 1, amount: 10 },
level2: { userId: 2, amount: 20 },
level3: { userId: 3, amount: 13 },
}
},
{
pid: qwsdfg,
qty: 1,
LevelDetails: {
level1: { userId: 1, amount: 10 },
level2: { userId: 4, amount: 20 },
level3: { userId: 3, amount: 13 },
}
},
]
From the collection, I need the sum of Level 1, Level 2, and Level 3 for each user.
The query result should look like this:
[
{ userId1: { TotalLevel1Amount: 20, TotalLevel2Amount: 0, TotalLevel3Amount: 0 } },
{ userId2: { TotalLevel1Amount: 0, TotalLevel2Amount: 20, TotalLevel3Amount: 0 } },
{ userId3: { TotalLevel1Amount: 0, TotalLevel2Amount: 0, TotalLevel3Amount: 26 } },
{ userId4: { TotalLevel1Amount: 0, TotalLevel2Amount: 20, TotalLevel3Amount: 0 } }
]
Solution 1:[1]
$set
: Add new field_levelDetails
by convertingLevelDetails
to key-value pair.$unwind
: Deconstruct_levelDetails
array.$group
: Group by_levelDetails.v.userId
and$sum
conditionally based on level (_levelDetails.k
).$project
: Format displayed document.$sort
(Optional): Sort byuserID
ascending.
db.collection.aggregate([
{
$set: {
_levelDetails: {
$objectToArray: "$LevelDetails"
}
}
},
{
$unwind: "$_levelDetails"
},
{
$group: {
_id: "$_levelDetails.v.userId",
"TotalLevel1Amount": {
$sum: {
$cond: [
{
"$eq": [
"$_levelDetails.k",
"level1"
]
},
"$_levelDetails.v.amount",
0
]
}
},
"TotalLevel2Amount": {
$sum: {
$cond: [
{
"$eq": [
"$_levelDetails.k",
"level2"
]
},
"$_levelDetails.v.amount",
0
]
}
},
"TotalLevel3Amount": {
$sum: {
$cond: [
{
"$eq": [
"$_levelDetails.k",
"level3"
]
},
"$_levelDetails.v.amount",
0
]
}
}
}
},
{
$project: {
_id: 0,
userId: "$_id",
TotalLevel1Amount: 1,
TotalLevel2Amount: 1,
TotalLevel3Amount: 1
}
},
{
$sort: {
userId: 1
}
}
])
To Key-Value Pair:
{ 'userId': { // Result } }
Steps 1 to 3 are the same as the previous solution.
$sort
(Optional): Sort by_id
ascending.$project
: Display document witharray
field (with propertiesk
andv
).$replaceRoot
: Replace entire documents to key (userId
) and value (result).
db.collection.aggregate([
{
$set: {
_levelDetails: {
$objectToArray: "$LevelDetails"
}
}
},
{
$unwind: "$_levelDetails"
},
{
$group: {
_id: "$_levelDetails.v.userId",
"TotalLevel1Amount": {
$sum: {
$cond: [
{
"$eq": [
"$_levelDetails.k",
"level1"
]
},
"$_levelDetails.v.amount",
0
]
}
},
"TotalLevel2Amount": {
$sum: {
$cond: [
{
"$eq": [
"$_levelDetails.k",
"level2"
]
},
"$_levelDetails.v.amount",
0
]
}
},
"TotalLevel3Amount": {
$sum: {
$cond: [
{
"$eq": [
"$_levelDetails.k",
"level3"
]
},
"$_levelDetails.v.amount",
0
]
}
}
}
},
{
$sort: {
_id: 1
}
},
{
$project: {
array: [
{
k: {
$toString: "$_id"
},
v: {
TotalLevel1Amount: "$TotalLevel1Amount",
TotalLevel2Amount: "$TotalLevel2Amount",
TotalLevel3Amount: "$TotalLevel3Amount"
}
}
]
}
},
{
"$replaceRoot": {
newRoot: {
$arrayToObject: "$array"
}
}
}
])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |