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

115
Vistas
Add aggregation by the condition of the value of your own variable

I have two functions to get an array from monga using aggregation. The functions are completely the same, except for one pipeline - "match ({startTime: {$ gte: start}})". How can I leave one function and add a "math" only by the presence of the "start" variable, which is a date filter?

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

The functions are completely the same,

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 Respuestas
Responde la pregunta

0

You can break the pipeline into a "head" and a "tail" and construct the head differently when the start argument is given:

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