Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

245
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda