javascript - 蒙戈数据库 : Find and insert if time slots doesn't conflict with other time slots

标签 javascript node.js mongodb mongoose

我有一个事件架构:

owner: {
    type: Schema.Types.ObjectId,
    ref: 'User',
    required: true
},
participants: [
    {
        type: Schema.Types.ObjectId,
        ref: 'User'
    }
],
createdOn: { type: Date, default: Date.now },
lastModified: { type: Date, default: Date.now },
active:{ type: Boolean, default: false },
eventDate: { type: Date },
startAt: { type: Date },
endAt : { type: Date },
timeZone:{ type: 'String', required: true }

我想插入事件,使这些事件不会与其他事件重叠(例如:如果某个事件发生在 2018 年 2 月 14 日,从凌晨 3:00 开始,到凌晨 4:00 结束,则不应有任何事件此日期凌晨 3:00 至凌晨 4:00 之间(除此事件外)。但此日期其他时间段可能有事件,但不得在凌晨 3:00 至凌晨 4:00 之间举办)

我尝试了以下逻辑,但似乎不起作用。这个逻辑有什么问题:

const eventData = {
    owner: req
        .body
        .owner
        .trim(),
    startAt: setTimeToDate(req.body.eventDate, req.body.startAt.trim(), req.body.timeZone),
    endAt: setTimeToDate(req.body.eventDate, req.body.endAt.trim(), req.body.timeZone),
    eventDate: moment(req.body.eventDate).toISOString(),
    participants: req.body.participants,
    active: true,
    timeZone: req.body.timeZone
};

Events.find({
    $and: [
        {
            "eventDate": new Date(eventData.eventDate)
        }, {
            $or: [
                {
                    $and: [
                        {
                            "startAt": {
                                $lt: new Date(eventData.startAt)
                            }
                        }, {
                            "startAt": {
                                $lte: new Date(eventData.endAt)
                            }
                        }
                    ]
                }, {
                    $and: [
                        {
                            "endtAt": {
                                $gte: new Date(eventData.startAt)
                            }
                        }, {
                                "endtAt": {
                                    $gt: new Date(eventData.endAt)
                                }
                            }
                        ]
                }
                ]
        }

        ]
})
    .exec(function (err, results) {
        const count = results.length;
        if (!count) {
            const newEvent = new Events(eventData);
            newEvent.save((err) => {
                if (err) {
                    res
                        .status(400)
                        .json({success: false, message: 'Could not create this availability.'});
                }
                res
                    .status(200)
                    .send({success: true, message: 'Your availability on this date is created successfully.'});
            });
        }

    });

最佳答案

所以我所做的就是找出一个事件与其他事件重叠的机会。

第一种情况是我要插入的新事件的开始时间位于任何其他事件的开始时间和结束时间之间。

第二种情况类似,唯一的区别是这次我检查要插入的新事件的结束时间是否在任何其他事件的开始时间和结束时间之间。

第三种情况是新事件的开始时间小于任何其他事件的开始时间并且新事件的结束时间大于任何其他事件的结束时间。

以上3种场景涵盖了所有时隙重叠的情况 其查询如下所示

{
            $or: [
                {
                    $and: [
                        {
                            "startAt": {
                                $lte: new Date(new Date(eventData.startAt))
                            }
                        }, {
                            "endAt": {
                                $gte: new Date(new Date(eventData.startAt))
                            }
                        }
                    ]
                }, {
                    $and: [
                        {
                            "startAt": {
                                $lte: new Date(new Date(eventData.endAt))
                            }
                        }, {
                            "endAt": {
                                $gte: new Date(new Date(eventData.endAt))
                            }
                        }
                    ]
                }, {
                    $and: [
                        {
                            "startAt": {
                                $gte: new Date(new Date(eventData.startAt))
                            }
                        }, {
                                "endAt": {
                                    $lte: new Date(new Date(eventData.endAt))
                                }
                            }
                        ]
                }
                ]
        }

关于javascript - 蒙戈数据库 : Find and insert if time slots doesn't conflict with other time slots,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48782039/

相关文章:

node.js - 在 NestJS 应用程序中的 Newrelic 中注释匿名中间件

php - 直接获取 MongoDB\Driver\Cursor 对象到指定类中

MongoDB 2.2 - 更新数组嵌套文档

javascript - 纯javascript可拖动元素

javascript - div 上层树没有收到点击 'through' lower div

java - Apache Camel 脚本问题

javascript - 如何将对象中的键与给定的字符串进行匹配?

javascript - pug-根据变量中是否存在元素执行操作

javascript - jQuery 模态弹出窗口 - 回发后输入类型设置为 null

mongodb - 为什么在使用 Homebrew 安装 MongoDB 时 sqlite 是 MongoDB 依赖项