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

123
Views
Múltiples agrupaciones para MongoDB Aggregate

Dado el siguiente conjunto de datos de objetos de evento:

 [ { "_id": ObjectId("4fda05cb322b1c95b531ac26", "title": "BUTTON CLICKED", "createdAt": ISODate("2017-01-12T01:00:00+01:00") }, { "_id": ObjectId("1235h1k235h1kl325h1v31gv", "title": "BUTTON CLICKED", "createdAt": ISODate("2017-01-14T01:00:00+01:00") }, { "_id": ObjectId("c2n890904cn897qnxp23hjk1", "title": "PAGE VIEWED", "createdAt": ISODate("2017-01-12T02:00:00+01:00") } ]

¿Cómo los agruparía por fecha y luego por nombre?

El resultado deseado se vería así:

 [ { _id: { year: 2017, month: 1, day: 11 }, events: [ { title: "BUTTON PRESSED", count: 3 }, { title: "PAGE VIEWED", count: 2 } ] }, { _id: { year: 2017, month: 1, day: 24 }, events: [ { title: "BUTTON PRESSED", count: 1 } ] } ]

Cualquier ayuda sobre este problema sería muy apreciada, ¡así que gracias!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

puedes probar esta consulta

 db.collectionName.aggregate([ $group: { _id : { year : { $year : "$createdAt" }, month : { $month : "$createdAt" }, day : { $dayOfMonth : "$createdAt" }, title: "$title" }, count:{$sum:1} } }, { $group:{ _id:{ year: "$_id.year", month: "$_id.month", day: "$_id.day" }, data:{ $push: { name:"$_id.title", count:"$count" } } } } ])
over 4 years ago · Santiago Trujillo 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!