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

183
Visualizações
Mongoose lte method not working for moment

I am fetching Date saved in db. Then, I am doing a small date maths to substract date from today from 3, which is giving me Date in Format - (DD-MM-YYYY). Date saved in db format is also same - (DD-MM-YYYY). Can anyone help me out in validating $lte for that date. I am not getting any log for DipData.

nodeCron.schedule("* * * * *", async function () {
    var DateNow = await moment().subtract(3, "days").format("DD-MM-YYYY");
     console.log("Test Date Cron",DateNow);
    console.log("-->",new Date(DateNow.format("DD-MM-YYYY")));

    let DipData = await userModel.find({}, { LastAppOpenedTime: { $lte : new Date(DateNow.format("DD-MM-YYYY")) }})
    console.log("-----DipData ------->", DipData);
   
});
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

First thing you need to identify if there is date which is stored in document of mongo collection is string or regular date format or epoch format. If it's string the query may gives not accurate result. If there is date format or epoch format, you can easily queried your result with proper result.

  1. Therefore in case if there is string in LastAppOpenedTime document key you can have query with $toDate under find query.
  2. If key is not in string format in stored document following code will work.
var DateNow = moment().subtract(3, "days");
const DipData = await userModel.find({ LastAppOpenedTime: { $lte: new Date(DateNow) } });

For the above two scenario would work if your query is in accurate form like removing the first empty braces.

userModel.find({}, { LastAppOpenedTime: { $lte : new Date(DateNow) }})

to

userModel.find({ LastAppOpenedTime: { $lte : new Date(DateNow) }})
about 4 years ago · Juan Pablo Isaza Relatório

0

Hello I got this working by making a few changes

    const DateNow = await moment().subtract(3, "days");
    console.log("Test Date Cron", DateNow);
    console.log("-->", new Date(DateNow));

    const DipData = await userModel.find({ createdAt: { $lte: new Date(DateNow) } });

    console.log("-----DipData ------->", DipData);
    res.status(200).json({ success: true, message: "Request was successful", DipData });

I noticed you had the .format("DD-MM-YYYY") at the end of your moment function but it returned a string that couldn't be converted with the new Date(DateNow). I removed mine when testing as the response from the moment was working fine without it.

And also I updated your userModel.find({}, { LastAppOpenedTime: { $lte : new Date(DateNow.format("DD-MM-YYYY")) }}) to remove the first empty {}. So you would have userModel.find({ createdAt: { $lte: new Date(DateNow) } });

about 4 years ago · Juan Pablo Isaza 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