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

98
Visualizações
MongoDB find all documents where field 1 is seven days or more after field 2

I need to search through a large mongo collection and find all the documents where updatedAt is at least 7 days after createdAt.

My data looks basically like this:

"createdAt" : ISODate("2021-04-03T10:17:21.256Z"),
"updatedAt" : ISODate("2021-04-03T10:17:21.256Z")

I would appreciate any help.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Use $expr in match. $dateAdd is only available in mongodb 5.0.

db.collection.aggregate([
  {
    "$match": {
      $expr: {
        $gt: [
          "$updatedAt",
          {
            $dateAdd: {
              startDate: "$createdAt",
              unit: "day",
              amount: 7
            }
          }
        ]
      }
    }
  }
])

mongoplayground


Use $expr in match. $add .

604800000 = 7 * 24 * 60 * 60 * 1000

db.collection.aggregate([
  {
    "$match": {
      $expr: {
        $gt: [
          "$updatedAt",
          {
            $add: [
              "$createdAt",
              604800000
            ]
          }
        ]
      }
    }
  }
])

mongoplayground


use $where

db.collection.find({
  "$where": "this.updatedAt > new Date(this.createdAt.getTime() + 604800000)"
})

mongoplayground

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