简体   繁体   English

如何使用mongoose中的find函数在mongodb中编写or语句

[英]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.不确定如何使用 mongoose 在 mongodb 中使用 or 条件。

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

You can simply use the or() member function that mongoose provides.您可以简单地使用 mongoose 提供的or()成员函数。 It works just like the $or operator defined in the mongoose documentation .它的工作方式与 mongoose 文档中定义的$or运算符一样。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM