Hello community,
Despite the few hours spent searching here and there for a solution, I cannot get around. Here is my document structure:
"CalculationDetails": [
{
"Code": "STEP_1_LABEL",
"Parameters": {
"PARAM_1_LABEL": {
"string": "5.88"
},
"PARAM_2_LABEL": {
"string": "5.0"
},
"PARAM_3_LABEL": {
"string": "0.005"
}
},
"Value": 0.004900000058114529
},
{
"Code": "STEP_2_LABEL",
"Parameters": {"PARAM_1_LABEL": {
"string": "5.88"
},
"PARAM_4_LABEL": {
"string": "0.001234"},
"Value": 5.881234
},
Main problematic: using the aggregation framework, I aim to build the following strucuture:
"steps":
{
"STEP_1_LABEL": 0.004900000058114529,
"STEP_2_LABEL": 5.881234,
}
I tried my way around $ObjectToArray, $ArrayToObject and $map, but couldn’t get to a conclusive result because my original document doesn’t have a “k”, “v” shape. I tried renaming “Code” into “k” and “Value” into “v”, but unsuccessfully.
Other information: I’m trying to get to my result without using $unwind, because my collection is already 2M documents large, and each document may contain up to 25 steps. It’s not a problem for MongoDB per se, but it is for PowerBI when I export data using the BI connector (very poor performance).
Also, I’m trying to avoid using $unwind + $group, because Compass can’t deal with such an amount of data with $group, at least with an M30 cluster.
Any idea how to reach the desired output given the constraints above?
Bonus question: ideally, I also wish my output to contain a second object:
"params":
{
"PARAM_1_LABEL": "5.88",
"PARAM_2_LABEL": "5.00",
"PARAM_3_LABEL": "0.005",
"PARAM_4_LABEL": "0.001234",
}
Parameters may appear multiple times (up to one time per step), however I require to output them only once.
For the same reason as stated above, I wish to avoid using an $unwind + $group (even though it would do the job) because the amount of data crashes Compass
Thank you very much for your help and happy new year everyone!