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

189
Views
How to group data by key without $unwind nor $group stage in mongodb aggregation

Can anyone help me to group the data by key without using $unwind nor $group stage ? The reason why I don't want to use $unwind nor $group stage is because I want to be able to use the pipeline in a updateMany(filter, pipeline) operation so the stages I can use are limited to: $addFields (= $set), $project (= $unset) and $replaceRoot (= $replaceWith).

The input data looks like this:

[
  {
    key: "alpha",
    value: {
      name: "foo",
    },
  },
  {
    key: "beta",
    value: {
      name: "bar",
    },
  },
  {
    key: "alpha",
    value: {
      name: "baz",
    },
  },
]

The result I would like to get:

[
  {
    key: "alpha",
    values: [
      {
        name: "foo",
      },
      {
        name: "baz",
      },
    ],
  },
  {
    key: "beta",
    values: [
      {
        name: "bar",
      },
    ],
  },
]

I believe that it's doable by using $reduce but I'm newbie with mongodb aggregation so I'm struggling to accumulate objects in array conditionally.

Thanks

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

    [
        {
            $set: {
                keys: { $setUnion: [ "$array.key" ] }
            
            }
        },
 
        {
            $set: {
                newarray : {
                    $map : {
                        input : "$keys",
                        in : {
                            key : "$$this",
                            values : {
                                $map : {
                                    input : {
                                        $filter : {
                                            input : "$array",
                                            as : "elem",
                                            cond : {
                                                $eq : [
                                                    "$$elem.key",
                                                    "$$this"
                                                ]
                                            }
                                        }},
                                        as : "vals",
                                        in : {
                                            name : "$$vals.value.name"
                                        }
                                    }
                                
                            }
                        }
                    }
                }
            }
        }
    ]


Try it. https://mongoplayground.net/p/m1UUvt5QNgg Or more compact version: https://mongoplayground.net/p/bFrSiyc5nSc

over 4 years ago · Santiago Trujillo 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!