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

110
Views
Agregar agregación por la condición del valor de su propia variable

Tengo dos funciones para obtener una matriz de monga usando agregación. Las funciones son completamente las mismas, excepto por una canalización: "coincidir ({startTime: {$ gte: start}})". ¿Cómo puedo dejar una función y agregar una "matemática" solo por la presencia de la variable "inicio", que es un filtro de fecha?

 let groupedByUserSessions if (lastDay) { const start = getDateFromString(lastDay); groupedByUserSessions = await getValuesByDate(start) } else { groupedByUserSessions = await getAllValues(); }

Las funciones son completamente las mismas,

 function getValuesByDate(start) { return Sessions.aggregate() .match({ startTime: { $gte: start } }) .group({ _id: { departament: "$departament", userAdName: "$userAdName" }, cleanTime: { $sum: { $subtract: ["$commonTime", "$idlingTime"] } } }) .group({ _id: { departament: "$_id.departament"}, users: { $push: {value: '$cleanTime', name: '$_id.userAdName'} }, commonCleanTime: { $sum: "$cleanTime" } }) .project({ departament: '$_id.departament', users: '$users', commonCleanTime: '$commonCleanTime', performance: { $divide: [ "$commonCleanTime", { $size: "$users" }] } });

}

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

0

Puede dividir la tubería en una "cabeza" y una "cola" y construir la cabeza de manera diferente cuando se proporciona el argumento de start :

 function getValuesByDate(start) { let agg = Sessions.aggregate(); if (start) { agg = agg.match({ startTime: { $gte: start } }); } return agg .group({ _id: { departament: '$departament', userAdName: '$userAdName' }, cleanTime: { $sum: { $subtract: ['$commonTime', '$idlingTime'] } }, }) .group({ _id: { departament: '$_id.departament' }, users: { $push: { value: '$cleanTime', name: '$_id.userAdName' } }, commonCleanTime: { $sum: '$cleanTime' }, }) .project({ departament: '$_id.departament', users: '$users', commonCleanTime: '$commonCleanTime', performance: { $divide: ['$commonCleanTime', { $size: '$users' }] }, }); }
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!