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

240
Vistas
check an array of string value with array of object in mongodb

I have array of strings like this

let fromHour = ['2.5','3','3.5']
let toHour = ['2.5','3','3.5']

I have an array of object saved in mongoDB

timeRange = [
              {
                from:'2.5',
                to:'3'
              },
              {
                from:'3',
                to:'3.5'
               }

           ]

I want to check if any of my array of string value exist in that object value

I have tried this but it give me this error ( Unrecognized expression '$match' )

checkAppoint = await Appointment.aggregate([
                {
                  $project: {
                    date: myScheduleFinal[k].date,
                    status: { $in: ['pending', 'on-going'] },
                    timeRange: {
                      '$match': {
                        'from': { $in: fromHolder },
                        'to': { $in: toHolder },
                      },
                    },
                  },
                },
              ]);

also I have tried this solution and it work for me but it take to much time so I am trying this with aggregate

 checkAppoint = await Appointment.findOne({
                date: myScheduleFinal[k].date,
                status: { $in: ['pending', 'on-going'] },
                timeRange:{$elemMatch:{
                  from:{$in:fromHolder},
                  to:{$in:toHolder}
                }}
              });

So anyone have a solution for that

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Just try $elemMatch and $in operators,

  • using find() method
checkAppoint = await Appointment.find({
  timeRange: {
    $elemMatch: {
      from: { $in: fromHour },
      to: { $in: toHour }
    }
  }
})

Playground


  • using aggregate() method
checkAppoint = await Appointment.aggregate([
  {
    $match: {
      timeRange: {
        $elemMatch: {
          from: { $in: fromHour },
          to: { $in: toHour }
        }
      }
    }
  }
])

Playground

over 4 years ago · Santiago Trujillo Denunciar

0

So I have found a way around to solve this problem and I will share the solution I used

First I want to minimize my request to mongodb so I am now making just one request that bring all the appointment with the required date and I want to make it this way because my fromHour and toHour array will change many time through single request

helperArray => contains all the day I want to check it's range

let checkAppoint = await Appointment.find({
      date: { $in: helperArray },
      status: { $in: ['pending', 'on-going'] },
    });

now inside my for loop I will go through that data

  checkAppoint.filter((singleAppoint) => {
    if (singleAppoint._doc.date === myScheduleFinal[k].date) {
      singleAppoint._doc.timeRange.map((singleTime) => {
          if (fromHolder.includes(singleTime.from)) {
            busy = true;
        }
      });
    }
  });
over 4 years ago · Santiago Trujillo 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