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

168
Views
MongoDB: Aggregate query is not passing value inside the function

I am facing a problem with the Mongoose aggregation query. I have the following schema which is an array of objects and contains the endDate value.

[
  {
    "id": 1,
    "endDate": "2022-02-28T19:00:00.000Z"
  },
  {
    "id": 2,
    "endDate": "2022-02-24T19:00:00.000Z"
  },
  {
    "id": 3,
    "endDate": "2022-02-25T19:00:00.000Z"
  }
]

So, during the aggregation result, I have to add a new field name isPast, It contains the boolean value, and perform the calculation to check if the endDate is passed or not. If it is already passed, then isPast will be true otherwise false.

I am using the isBefore function from the moment library which returns the boolean. But inside this function facing a problem regarding passing the endDate value. $endDate is passing as a string, not a value.

Is there a way to pass the value of endDate inside the function?

const todayDate = moment(new Date()).format("YYYY-MM-DD");

db.collection.aggregate([
  {
    $addFields: {
      "isPast": moment('$endDate', 'YYYY-MM-DD').isBefore(todayDate)
    },

  },

])
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can achieve without momentjs. Use $toDate to convert date-time string to date

db.collection.aggregate([
  {
    $addFields: {
      "isPast": {
        $gt: [
          new Date(),
          {
            $toDate: "$endDate"
          }
        ]
      }
    }
  }
])

Sample Mongo Playground


If you just want to compare for date only:

db.collection.aggregate([
  {
    $set: {
      "currentDate": "$$NOW",
      "endDate": {
        $toDate: "$endDate"
      }
    }
  },
  {
    $addFields: {
      "isPast": {
        $gt: [
          {
            "$dateFromParts": {
              "year": {
                $year: "$currentDate"
              },
              "month": {
                $month: "$currentDate"
              },
              "day": {
                "$dayOfMonth": "$currentDate"
              }
            }
          },
          {
            "$dateFromParts": {
              "year": {
                $year: "$endDate"
              },
              "month": {
                $month: "$endDate"
              },
              "day": {
                "$dayOfMonth": "$endDate"
              }
            }
          }
        ]
      }
    }
  }
])

Sample Mongo Playground (Compare date only)

over 4 years ago · Santiago Trujillo 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!