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

102
Views
Agregar campo a cada objeto de una matriz en función de otro campo

Tengo la siguiente matriz de datos:

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

Lo que necesito es agregar el valor correspondiente a cada uno de los objetos attr1 según el índice. Entonces el resultado sería:

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

Me pregunto si eso es posible usando la agregación $addFields en MongoDB

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

0

Puedes usar $zip

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

Aquí está el parque infantil de Mongo para su referencia.

about 4 years ago · Juan Pablo Isaza Report

0

Consulta

  • la consulta funciona si las matrices son del mismo tamaño
  • ziparray para hacer [[member1_1 member2_1], ....]
  • mapa para fusionar miembro1_1, miembro2_1 a un documento

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 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!