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

120
Visualizações
Using math function in projections in mongodb

Using Node and Mongoose Trying to fetch data from a collection in which field days is having negative as well as positive numbers stored. I want to show all the data of days in positive only, so therefore using Math abs function. but somehow it is not working and also not showing any error.

Code is below:

        let projection = {
        _id : 0,
        userId : 1,
        sum : 1,
        days : Math.abs("$days"), //This is not working
        leaveType : 1,
        reason : 1,
        fromDate : 1,
        toDate : 1,
        leaveDuration : 1,
        leaveStatus : 1,
        createdAt : 1,
        updatedAt : 1,
        rejectReason : 1,
        empName : "$userDetails.fullName",
        leaveType : "$LeaveDetails.leaveTitle",
        availableBalance : 1,
    }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

$abs (aggregation) is what you are looking for. Pls check the link below:

https://docs.mongodb.com/manual/reference/operator/aggregation/abs/

  let projection = {
    _id : 0,
    userId : 1,
    sum : 1,
    days: {$abs: "$days" } , //This will work
    leaveType : 1,
    reason : 1,
    fromDate : 1,
    toDate : 1,
    leaveDuration : 1,
    leaveStatus : 1,
    createdAt : 1,
    updatedAt : 1,
    rejectReason : 1,
    empName : "$userDetails.fullName",
    leaveType : "$LeaveDetails.leaveTitle",
    availableBalance : 1
}
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