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

151
Views
How to write an or statement in mongodb using the find function in mongoose

I'm trying to check if there is a booking in a range of time. Not sure how to use the or condition in mongodb using mongoose.

Here is what I have:

const appoinments = await Appointment.find({
      seller: seller._id,
      startTime: {
        $gt: ISODate(new Date(req.body.startTime).toISOString),
        $lt: ISODate(new Date(req.body.endTime).toISOString),
      },// I WANT TO ADD THE OR TO THIS TWO QUERIES
      endTime: {
        $gt: ISODate(new Date(req.body.startTime).toISOString),
        $lt: ISODate(new Date(req.body.endTime).toISOString),
      },
    });

I want to check and find for any appointment that conflict with new appointment that is about to be added or any appointment that ends during that time of the appointment.

Here is what my Appointment looks like:

 const appointmentSchema = new mongoose.Schema({
    seller: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "Seller",
     },
    startTime: {
        type: Date,
        required: true,
      },
    endTime: {
        type: Date,
      },
  });
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can simply use the or() member function that mongoose provides. It works just like the $or operator defined in the mongoose documentation.

I hope it helped!

const appoinments = await Appointment.find().or([
    {
        startTime: {
            $gt: ISODate(new Date(req.body.startTime).toISOString),
            $lt: ISODate(new Date(req.body.endTime).toISOString),
        }
    },
    {
        endTime: {
            $gt: ISODate(new Date(req.body.startTime).toISOString),
            $lt: ISODate(new Date(req.body.endTime).toISOString),
        },
    }
]);
about 4 years ago · Juan Pablo Isaza 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!