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

116
Views
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 answers
Answer question

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