Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

114
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda