[英]How to write an or statement in mongodb using the find function in mongoose
我正在嘗試檢查在某個時間范圍內是否有預訂。 不確定如何使用 mongoose 在 mongodb 中使用 or 條件。
這是我所擁有的:
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),
},
});
我想檢查並查找與即將添加的新約會或在約會期間結束的任何約會相沖突的任何約會。
這是我的約會的樣子:
const appointmentSchema = new mongoose.Schema({
seller: {
type: mongoose.Schema.Types.ObjectId,
ref: "Seller",
},
startTime: {
type: Date,
required: true,
},
endTime: {
type: Date,
},
});
您可以簡單地使用 mongoose 提供的or()
成員函數。 它的工作方式與 mongoose 文檔中定義的$or
運算符一樣。
我希望它有幫助!
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.