Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

104
Vistas
Add field to each object of an array based on other field

I have the following Array of data:

    {
            _id: 5f5726ef7d475a61a95c5e0c,
            attributes: [
                {
                    values: [
                        { name: '1' }
                    ],
                },
                { 
                    values: [
                        { name: '2' }
                    ]
                }
            ],
            attr1: [
                { name: "Study Code" },
                { name: "Patient Study" }
            ]
    }

What I need is to add the correspondent value to each on of attr1 objects based on index. So the result would be:

    {
            _id: 5f5726ef7d475a61a95c5e0c,
            attributes: [
                {
                    values: [
                        { name: '1' }
                    ],
                },
                { 
                    values: [
                        { name: '2' }
                    ]
                },
            ],
            attr1: [
                {
                    name: "Study Code",
                    values: [{ name: "1" }]
                },
                {
                    name: "Patient Study",
                    values: [{ name: "2" }]
                }
            ],
    }

I wonder if that possible using aggregation $addFields in MongoDB

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can use $zip

db.collection.aggregate([
  {
    "$project": {
      attributes: {
        "$zip": {
          "inputs": [
            "$attributes",
            "$attr1"
          ]
        }
      }
    }
  }
])

Here is the Mongo playground for your reference.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Query

  • query works if arrays same size
  • ziparray to make [[member1_1 member2_1], ....]
  • map to merge member1_1,member2_1 to a document

Playmongo

aggregate(
[{"$set": {"attr1": {"$zip": {"inputs": ["$attributes", "$attr1"]}}}},
 {"$set": 
   {"attr1": 
     {"$map": 
       {"input": "$attr1",
        "in": 
         {"$mergeObjects": 
           [{"$arrayElemAt": ["$$this", 1]},
             {"$arrayElemAt": ["$$this", 0]}]}}}}}])
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda