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

239
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 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