Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

237
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!