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

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

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