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

390
Views
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 answers
Answer question

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