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

139
Visualizações
check date existence on array of string

This is my dataset from MongoDB Collection:


{
  "_id": "60c0ace96e93993880efd337",
  "lv": [
   uid: 1,
   "createdate": "2021-12-15T12:30:01.935Z",
   "updatedAt": [
      "2021-12-15T12:31:11.635Z",
      "2021-12-15T12:31:51.955Z",
      "2021-12-16T12:30:01.935Z",
      "2021-12-16T12:30:01.935Z",
      "2021-12-17T12:30:01.935Z",
      "2021-12-18T12:30:01.935Z"
   ]
  ]
},
{
...
}

I want to filterout only the data which date range lies in createdate column or updatedAt column.

I am not able to get the desired result. Not getting the idea that where I am making the mistake in the query or coed.

What I have tried I will mention here.

let startA = new Date("2021-12-14");
const a = new Date(startA.setHours(startA.getHours() - 5));
const start = new Date(a.setMinutes(startA.getMinutes() - 30));

let endA = new Date("2021-12-17");
const b = new Date(endA.setHours(endA.getHours() - 5));
const end = new Date(b.setMinutes(endA.getMinutes() - 30));

const fetchData = await MyCollection.findOne(
  { 
    _id: ObjectId(req.body.id),
    'lv.createdate': { $gte: start, $lt: end },
    'lv.updatedAt': {
      $elemMatch: { $gte: start, $lt: end }
    }
  }
).lean().exec();

Any help or suggestion is really appreciated. Thanks in advance for the interaction.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your input data is not valid. With some assumptions, the solution could be this one:

db.MyCollection.aggregate([
   {
      $set: {
         lv: {
            $map: {
               input: "$lv",
               as: "lv",
               in: {
                  $filter: {
                     input: "$$lv.updatedAt",
                     cond: {
                        $and: [
                           { $gte: ["$$this", start] },
                           { $lt: ["$$this", end] }
                        ]
                     }
                  }
               }
            }
         }
      }
   }
])

or this one:

db.MyCollection.aggregate([
   {
      $set: {
         "lv.updatedAt": {
            $filter: {
               input: "$lv.updatedAt",
               cond: {
                  $and: [
                     { $gte: ["$$this", start] },
                     { $lt: ["$$this", end] }
                  ]
               }
            }
         }
      }
   }
])
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