I have a collection that’s structure is somewhat similar to the following:-
Initial:
{alphabets1:
[{a:value11},
{b:value12},
{c:value13},...]
},
{alphabets2:
[{a:value21},
{b:value22},
{c:value23},...]
}
I need to get the following information from the above collection:
Required:
{alphabets1:value11+value12+value13...},
{alphabets2:value21+value22+value23...}
To get the above structure above, I have to modify the documents structure in the array to the following:
{alphabets1:[{fieldName:a,value:value11},
{fieldName:b,value:value12},
{fieldName:c,value:value13},...]},
{alphabets2:[{fieldName:a,value:value21},
{fieldName:b,value:value22},
{fieldName:c,value:value23},...]},
and use $group
aggregate.
And for this, I have to access the field name, but I couldn’t find a way to do it in mongodb and I was wondering if there are any other ways to get the Required format from the Initial document structure.