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

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

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