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

169
Visualizações
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 Respostas
Responde à pergunta

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