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

461
Visualizações
MongoDB - How to get a date that is between two dates

I am creating a database for a dentist. On Tuesdays, the secretary sends reminders to all the patients who have an appointment for the next week.

The query I used in SQL is:

SELECT P.Name    
    , P.Tel_No    
    , A.Appointment_Time    
    , A.Appointment_Date    
FROM Patient AS P    
    INNER JOIN Booking AS B ON P.Patient_ID = B.Patient_ID    
    INNER JOIN Appointment AS A ON B.Appointment_ID = A.Appointment_ID    
WHERE A.Reminder_Sent = 0  
AND A.Appointment_Date BETWEEN     
    ADDDATE(DAY, 6, CURDATE()) AND ADDDATE(DAY, 9, CURDATE());

I want to follow the same logic as the last two lines. So Tuesday plus 6 days is the following Monday, and Tuesday plus 9 days is the following Friday. If the appointment falls between these two dates, then a reminder is sent.

How do I extend this logic to MongoDB?

I imagine it would be something like:

"Appointment Time" : $range{

{$dateAdd:{currentdate(),unit:day,amount:6}, 

{$dateAdd:{currentdate(),unit:day,amount:9}}

Or something like this. Any help would be appreciated!

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

0

You can use the $$NOW variable, like so:

db.collection.aggregate([
  {
    $match: {
      $expr: {
        $and: [
          {
            $gt: [
              "$Appointment_Date",
              {
                $add: [
                  "$$NOW",
                  518400000 // 6 days in miliseconds
                ]
              }
            ]
          },
          {
            $lt: [
              "$Appointment_Date",
              {
                $add: [
                  "$$NOW",
                  777600000 // 9 days in miliseconds
                ]
              }
            ]
          }
        ]
      }
    }
  }
])

Mongo Playground

If you're using Mongo version 5+ this query can be simplified by using $dateAdd, I just assumed you're not on such an advanced version.

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