Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
How can I apply aggregate on array of objects?

I have this complicated data in MongoDB, I want to reconstruct it into something like the second JSON, I tried both MongoDB aggregate and javascript spread, but I couldn't figure it out.

Original Data

[
  {
    "_id": "61ff24b2db73094dd3029c7e",
    "TeamName": "GoodTeam",
    "TeamImage": "Avatar",
    "TeamMember": [
      {
        "name": "Aimee",
        "image": "https://i.imgur.com/HeIi0wU.png",
        "email": "test2@gmail.com",
        "timezone": "PST",
        "_id": "61ff25fcd2a0b9e13ee8989c"
      },
      {
        "name": "Kai",
        "image": "https://i.imgur.com/HeIi0wU.png",
        "email": "school021195@gmail.com",
        "timezone": "PST",
        "_id": "61ff3114d3d343bd673f5ad3"
      },
      {
        "name": "Iren",
        "image": "https://i.imgur.com/HeIi0wU.png",
        "email": "test1@gmail.com",
        "timezone": "AKST",
        "_id": "61ff3114d3d343bd673f5ad4"
      }
    ],
    "createdAt": "2022-02-06T01:30:26.893Z",
    "updatedAt": "2022-02-06T02:23:16.660Z",
    "__v": 0
  }
]

Reconstruct Data

[
  { 
    "timezone":"PST",
    "TeamMember":[
       {
        "name": "Aimee",
        "image": "https://i.imgur.com/HeIi0wU.png",
        "email": "test2@gmail.com",
        "timezone": "PST",
        "_id": "61ff25fcd2a0b9e13ee8989c"
      },
      {
        "name": "Kai",
        "image": "https://i.imgur.com/HeIi0wU.png",
        "email": "school021195@gmail.com",
        "timezone": "PST",
        "_id": "61ff3114d3d343bd673f5ad3"
      },
  {
    "timezone":"AKST",
    "TeamMember":[
      {
        "name": "Iren",
        "image": "https://i.imgur.com/HeIi0wU.png",
        "email": "test1@gmail.com",
        "timezone": "AKST",
        "_id": "61ff3114d3d343bd673f5ad4"
      }
    ]
  }
]

And how should I deal with this kind of data reconstruction in the front-end or back-end? Which one is better practice?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

It is better to do it in the backend and recommended.

You can achieve this by multiple ways in the backend using mongo aggregation framework. It is very powerful framework with multiple operators to handle these kind of data.

about 4 years ago · Juan Pablo Isaza Report

0

You can always create a custom transformer for converting one JSON to another JSON and I would suggest you to let the back-end handle this responsibility.

Below is one example using Javascript.

const firstJson = `[
    {
      "_id": "61ff24b2db73094dd3029c7e",
      "TeamName": "GoodTeam",
      "TeamImage": "Avatar",
      "TeamMember": [
        {
          "name": "Aimee",
          "image": "https://i.imgur.com/HeIi0wU.png",
          "email": "test2@gmail.com",
          "timezone": "PST",
          "_id": "61ff25fcd2a0b9e13ee8989c"
        },
        {
          "name": "Kai",
          "image": "https://i.imgur.com/HeIi0wU.png",
          "email": "school021195@gmail.com",
          "timezone": "PST",
          "_id": "61ff3114d3d343bd673f5ad3"
        },
        {
          "name": "Iren",
          "image": "https://i.imgur.com/HeIi0wU.png",
          "email": "test1@gmail.com",
          "timezone": "AKST",
          "_id": "61ff3114d3d343bd673f5ad4"
        }
      ],
      "createdAt": "2022-02-06T01:30:26.893Z",
      "updatedAt": "2022-02-06T02:23:16.660Z",
      "__v": 0
    }
  ]`;

  const firstJsonObj = JSON.parse(firstJson);
  
  const map = new Map();
  for(let i =0;i<firstJsonObj.length;i++){
    for(let j =0;j<firstJsonObj[i].TeamMember.length;j++){
        let timezone = firstJsonObj[i].TeamMember[j].timezone;
        if(map.has(timezone)){
            const team = map.get(timezone);
            team.push(firstJsonObj[i].TeamMember[j]);
            map.set(timezone,team);
        }
        else{
            map.set(timezone,[firstJsonObj[i].TeamMember[j]])
        }
    }
  }

  const secondJson = Array.from(map,function(element){
      const [key,value] = element;
   return {
        timezone : key,
        TeamMember : value
    }
  });
  console.log(JSON.stringify(secondJson));

EDIT: If you want query data from mongodb itself you can use below query to get the output

 db.collection.aggregate([
  {
    "$unwind": "$TeamMember"
  },
  {
    "$group": {
      "_id": {
        "timezone": "$TeamMember.timezone"
      },
      "TeamMember": {
        "$push": "$$ROOT.TeamMember"
      }
    }
  },
  {
    "$project": {
      "_id": 0,
      "timezone": "$_id.timezone",
      "TeamMember": 1
    }
  }
])
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!