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

211
Vistas
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,
      },
  });
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

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),
        },
    }
]);
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