title: how to get other elements along with arrayToObject in mongodb?
body: <p>I am trying to get the count of all the different value of a key in my MongoDB. I am getting the count as well but I need other details as well along with the count.</p>
  
  <pre><code>   {hobby:"swimming", "name" : "abc", "state" : 'PA', "age":10, "address":"ggg"}
     {hobby:"swimming", "name" : "abc", "state" : 'PA', "age":10, "address":"ggg" }
     {hobby:"swimming", "name" : "def", "state" : 'IA', "age":12, "address":"kkk"  }
     {hobby:"swimming", "name" : "def", "state" : 'IA', "age":12, "address":"kkk"   }
     {hobby:"swimming", "name" : "abc", "state" : 'DA', "age":10, "address":"ggg"  }
     {hobby:"swimming", "name" : "abc", "state" : 'DA', "age":10, "address":"India"  }
     {hobby:"swimming", "name" : "def", "state" : 'DA', "age":12, "address":"kkk"   }
     {hobby:"swimming", "name" : "abc", "state" : 'IA', "age":10, "address":"ggg"  }
  
  
       return db.collection.aggregate([
          {"$match":{"hobby":"swimming"}},
          {"$group":
            {
               "_id":{"name":"$name","state":"$state","age":"$age","address":"$address"},
               "count":{"$sum":1}
             }
          },
          {"$group":{
              "_id":"$_id.name",
              "age":"$_id.age",
             "address":"$_id.address",
             "details":{
                  "$push":{
                     "k":"$_id.state",
                     "v":"$count"
                  }
               }
           }},
           {"$addFields":{
              "details":{
                  "$arrayToObject":"$details"
               }
            }}
        ])
  </code></pre>
  
  <p>Current Result:-</p>
  
  <p>Unhandled Rejection at: MongoError: The field 'age' must be an accumulator object</p>
  
  <p>Expected Result:-</p>
  
  <pre><code>[
    {
       "_id": "def",
       "age":10,
       "address":"ggg",
       "details": {
         "DA": 1,
         "IA": 2
       }
    },
   {
       "_id": "abc",
       "age":12,
       "address":"kkk",
       "details": {
          "DA": 2,
          "IA": 1,
          "PA": 2
       }
    }
  ]
  </code></pre>
  
  <p>Anyone any idea how to get this?</p>