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

109
Views
MongoDB - $ addToSet para una matriz?

Estructura:

 [ { "_id": "12", "title": "Vanella Icream", "contain":"sugar", "details": [ { "flavour": "Vanella" }, { "weight": "6KG" } ] }, { "_id": "3", "title": "Pretzels", "contain":"salt", "details": [ { "flavour": "Wheat" }, { "weight": "2KG" } ] } ]

Rendimiento esperado

 details: { "flavour": [ "Vanella", "Wheat", ], "weight": [ "6KG", "2KG", ] }

Más importante : tengo que hacer esto sin details.flavour o details.weight .

Lo intenté con $addToSet , $filter $group pero no obtengo resultados como ese. alguien por favor sugiera.

Zona de juegos de Mongo: https://mongoplayground.net/p/SXw3jaRDzLy

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

0

  • $project para mostrar el campo requerido
  • $unwind deconstruye la matriz de details
  • $objectToArray convierte el objeto de details en formato de clave-valor de matriz
  • $unwind deconstruye la matriz de details
  • $group por clave de detalles y construir la matriz de valores
  • $group por nulo y construir la matriz de details en formato clave-valor
  • $arrayToObject convierte el resultado anterior de la matriz de formato de clave-valor del objeto a un objeto
 db.collection.aggregate([ { $project: { _id: 0, details: 1 } }, { $unwind: "$details" }, { $project: { details: { $objectToArray: "$details" } } }, { $unwind: "$details" }, { $group: { _id: "$details.k", v: { $addToSet: "$details.v" } } }, { $group: { _id: null, details: { $push: { k: "$_id", v: "$v" } } } }, { $project: { _id: 0, details: { $arrayToObject: "$details" } } } ])

Patio de juegos

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!