Using a field name as another field value

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.

Hi @Vamsi_Kumar_Naidu_Pallapothula and welcome to MongoDB community forums!!

Could you elaborate on the requirement as this is not very clear from the above structure shared.
However, from my understanding, are you trying to add all the values for all the filenames .

Can you help me understand the use case in a more better way with

  1. A sample document
  2. The query you have tried.
  3. The desired output from the query.

Regards
Aasawari

Hi @Aasawari, thanks for responding to my query. I have solved this issue using $objectToArray operator. For the sake of completion I am adding the issue along with solution that I thought of below. As I am a beginner in mongoDB, I’m not sure if this is an optimal solution, so please feel free to add any other approaches that you might think of :slight_smile: :

Problem

I am working on a cricket related data. Following is the kind of structure that I have:

//overs: [ {over: runs scored in that over}...]
{
innings: 1,
overs: [{"0":5},{"1":9},{"2":8},...,{"19":13}]
}

and I wanted to know the total number of runs scored in the 20 overs, like shown below

{ innings:1, runs:5+9+8+...+13 }

Solution

using $objectToArray and $unwind I modified the initial structure to something that looks like this

{
{ innings:1, overs:{k:"0",v:5}},
{ innings:1, overs:{k:"1",v:9}},
...
{ innings:1, overs:{k:"19",v:13}},
}

then I used $group and $sum operators to get the total runs in an innings.

Code

This process can be replicated using the code provided below:

db.dummy.insertOne({
  "innings":1,
  "overs": [
    {"0":3}, {"1":4}, {"2":9}, {"3":3}, {"4":9}, {"5":19}, {"6":1}, {"7":11}, {"8":21}, {"9":11},
    {"10":3}, {"11":4}, {"12":9}, {"13":3}, {"14":4}, {"15":9}, {"16":11}, {"17":11}, {"18":22}, {"19":1}
  ]
})

db.dummy.find()

db.dummy.aggregate([
  {$unwind: "$overs"},
  {$project: {
    innings:"$innings",
    overs: {$objectToArray: "$overs"}
  }},
  {$unwind:"$overs"},
  {$group: {
    _id: {innings:"$innings"},
    runs: {
      $sum: "$overs.v"
    }
  }}
])

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.