Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

132
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda