Here is my response after some operations. Based on empid I want to group districts, products and get the count of sms and whatsapp.
[
  {
    "_id": {
      "empid": "sindhu",
      "district": "Hyderabad",
      "product": "Fair Fertilizers"
    },
    "sms": 1
  },
  {
    "_id": {
      "empid": "nagaraju",
      "district": "Guntur",
      "product": "Fair Fertilizers"
    },
    "sms": 2
  },
  {
    "_id": {
      "empid": "sindhu",
      "district": "Hyderabad",
      "product": "Fair Fertilizers"
    },
    "whatsapp": 2
  },
  {
    "_id": {
      "empid": "sindhu",
      "district": "Krishna",
      "product": "Fair Fertilizers"
    },
   "whatsapp": 2
  }
]
I want the above data in the format as shown below.
[
  {
    "sindhu":[
        {
          "district": "Hyderabad",
          "product": "Fair Fertilizers",
          "sms": 1,
          "whatsapp": 2
        },
        {
          "district": "Krishna",
          "product": "Fair Fertilizers",
          "whatsapp": 2
        },
    ]
  },
  {
    "nagaraju" : [
        {
          "district": "Guntur",
          "product": "Fair Fertilizers",
          "sms": 2
        }
     ]
  }
]
In order to get like that I tried the below code. But I didn't get it.
var groups = Object.create(null);
for (var i = 0; i < allTools.length; i++) {
    var item = allTools[i];
    if(!groups[item._id]) {
        groups[item._id] = [];
    }
    groups[item._id].push({
        district: item.district,
        product: item.product,
        sms: item.sms,
        whatsapp: item.whatsapp,
        mailing: item.mailing,
        telecalling: item.telecalling,
        enquiry: item.enquiry
    });
}
var data = [];
for (var x in groups) {
    var obj = {};
    obj[x] = groups[x];
    data.push(obj);
}
 
     
    