Wanted to find the sum of Nested json array via mongodb query:
$group is asking for _id for grouping each obj in array, which is not working.
The documents is like this :
{
point : "3",
created_date : "2022-06-19",
"name" : "grpA",
"value_format" : [
{
"data" : {
"A" : 10,
"B" : 20,
"C" : 30,
"D" : 40,
},
"key" : "key1"
},
"data" : {
"A" : 50,
"B" : 60,
"C" : 70,
"D" : 80,
},
"key" : "key2"
}
]
}
Expected output :
{
"key" : "anything",
"val" : {
"sumA": 60,
"sumB": 80,
"sumC": 100,
"sumD": 120,
}
}
Query1
aggregate(
[{"$set":
{"sumA": {"$sum": "$value_format.data.A"},
"sumB": {"$sum": "$value_format.data.B"},
"sumC": {"$sum": "$value_format.data.C"},
"sumD": {"$sum": "$value_format.data.D"}}}])
Query2
value_format
sumA : ...
arrayToObject
*most likely you need query2, the complexity comes from the data on schema, and this is kinda fast way, it could be worse
aggregate(
[{"$unwind": "$value_format"},
{"$project": {"data": {"$objectToArray": "$value_format.data"}}},
{"$unwind": "$data"},
{"$group": {"_id": "$data.k", "sum": {"$sum": "$data.v"}}},
{"$replaceRoot":
{"newRoot":
{"$arrayToObject":
[[{"k": {"$concat": ["sum", "$_id"]}, "v": "$sum"}]]}}},
{"$group": {"_id": null, "val": {"$mergeObjects": "$$ROOT"}}}])
Query3
one_doc_collection=[{}]
aggregate(
[{"$project":
{"value_format":
{"$map":
{"input": "$value_format",
"in": {"$objectToArray": "$$this.data"}}}}},
{"$set":
{"value_format":
{"$reduce":
{"input": "$value_format",
"initialValue": [],
"in": {"$concatArrays": ["$$value", "$$this"]}}}}},
{"$lookup":
{"from": "one_doc_collection",
"pipeline":
[{"$set": {"value_format": "$$value_format"}},
{"$unwind": "$value_format"},
{"$group":
{"_id": "$value_format.k", "sum": {"$sum": "$value_format.v"}}},
{"$replaceRoot":
{"newRoot":
{"$arrayToObject":
[[{"k": {"$concat": ["sum", "$_id"]}, "v": "$sum"}]]}}}],
"as": "sums",
"let": {"value_format": "$value_format"}}},
{"$project":
{"sums":
{"$reduce":
{"input": "$sums",
"initialValue": {},
"in": {"$mergeObjects": ["$$value", "$$this"]}}}}}])