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

237
Views
How do I create new field names using values from a list of objects in pymongo?

So I have records in a mongo database which are of this format (just an example):

{
    _id: 1,
    name: "some name1",
    description: "some description1",
    parameters: [
        {
            name: "param name1",
            type: "param type1",
            default: "default value1"
        },
        {
            name: "param name2",
            type: "param type2",
            default: "default value2"
        }
    ]
   .
   .
   .
}

In my program I am performing this aggregation on the records:

{
    '$project': {
        'title': '$name,
        'description': True,
        'parameters': {
            'name': True,
            'parameter_type': '$parameters.type',
            'value': '$parameters.default'
        }
    }
}

The result I was trying to get:

{
    _id: 1,
    title: 'some name1',
    description: "some description1",
    parameters: [ 
        {
            name: "param name1",
            parameter_type: "param type1",
            value: "default value1"
        },
        {
            name: "param name2",
            parameter_type: "param type2",
            value: "default value2"
        }
    ]
 }

Instead I got:

{
    _id: 1,
    title: 'some name1',
    description: "some description1",
    parameters: [ 
        {
            name: "param name1",
            parameter_type: ["param type1", "param type2"],
            value: ["default value1", "default value2"]
        }
    ]
 }

All am I trying to do is change the field names for the objects in the parameters list. I have tried looking through the MongoDB docs but I have not been able to find a way to do what I need.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

$unwind Deconstructs an array field from the input documents and group them back with desired projection.

Demo - https://mongoplayground.net/p/-pIPbSarfyx

db.collection.aggregate([
  {
    "$unwind": "$parameters"
  },
  {
    "$group": {
      "_id": "$_id",
      "name": {
        "$first": "$name"
      },
      "description": {
        "$first": "$description"
      },
      "parameters": {
        "$push": {
          "name": "$parameters.name",
          "parameter_type": "$parameters.type",
          "value": "$parameters.default"
        }
      },
      
    }
  }
])

$unwind

$group

$first

$push

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!