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

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

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