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

373
Vistas
Agregación de MongoDB: el campo debe ser un objeto acumulador en la fase de grupos

Soy nuevo en la agregación de mongodb. Digamos que tengo estos datos:

 [ { "end_year": 2020, "intensity": 12, "sector": "Information Technology", "topic": "robot", "insight": "62 market research reports study robotics industry", "url": "http://robohub.org/62-market-research-reports-study-robotics-industry/", "region": "", "start_year": 2016, "impact": "", "added": "July, 16 2016 02:44:49", "published": "July, 11 2016 00:00:00", "country": "", "relevance": 3, "pestle": "Economic", "source": "Robothub", "title": "Forecasts the industrial robotics market in APAC to grow at a CAGR of 8.7% during the period 2016-2020 in the top 5 segments.", "likelihood": 4 }, { "end_year": "", "intensity": 2, "sector": "Government", "topic": "government", "insight": "Oil, Greed, and Grievances in the Middle East and North Africa", "url": "https://www.newsecuritybeat.org/2016/07/oil-greed-grievances-middle-east-north-africa/", "region": "", "start_year": "", "impact": "", "added": "July, 16 2016 01:12:21", "published": "July, 12 2016 00:00:00", "country": "", "relevance": 1, "pestle": "Political", "source": "New Security Beat", "title": "Countries threatened by larger-scale insurgencies might want to consider giving regional groups some share in central government decision-making.", "likelihood": 2 } ]

Esta es mi consulta:

 // return all the countries associated with a single topic const data = await Data.aggregate([ { $match: {} }, { $group: { _id: "$topic",country: ["$country"],likelihood: ["$likelihood"],relevance: ["$relevance"],intensity: ["$intensity"] } } ])

Quiero devolver todos los países, probabilidades, relevancias asociadas con un tema único/común

Sé cómo obtener el primero y el último usando los operadores $primero y $último. ¿Cómo los consigo todos en una matriz?

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

0

Podemos usar el operador $push para agregar todos los valores de campo:

 const data = await Data.aggregate([ { $match: {} }, { $group: { _id: "$topic",data: { $push: { country: "$country",likelihood: "$likelihood",relevance: "$relevance",intensity: "$intensity" } } } } ])

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