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

166
Views
Consulta agregada de MongoDB, promedios de inicios de sesión
let pipeline = [{ $match: { time: { $gt: 980985600 }, user_id: mongoose.Types.ObjectId("60316a2e7641bd0017ced7b1") } }, { $project: { newDate: { '$toDate': "$time" }, user_id: '$user_id' } }, { $group: { _id: { week: { $week: "$newDate" }, year: { $year: "$newDate" }}, count: { $sum: 1 } } }]

Actualmente estoy tratando de realizar un agregado a través de mongoose para encontrar el promedio de inicios de sesión por semana para un usuario específico. Hasta ahora he podido llegar al número total de inicios de sesión cada semana, pero tenía curiosidad por saber si había alguna forma de encontrar el promedio de estos grupos finales dentro de la misma función. ¿Cómo haría para hacer esto?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Simplemente agregue una última etapa a su consulta:

 { $group: { _id: null, avg: { $avg: "$count" } } }

Así que prueba esto:

 let pipeline = [ { $match: { time: { $gt: 980985600 }, user_id: mongoose.Types.ObjectId("60316a2e7641bd0017ced7b1") } }, { $project: { newDate: { '$toDate': "$time" }, user_id: '$user_id' } }, { $group: { _id: { week: { $week: "$newDate" }, year: { $year: "$newDate" } }, count: { $sum: 1 } } }, { $group: { _id: null, avg: { $avg: "$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!