首页 > 解决方案 > 如何在 MongoDB 中为作为教师模式的一部分的时间表创建模式?

问题描述

这是我在 StackOverFlow 的第一篇文章,我来这里是为了寻找我正在开发的项目的一些想法。

首先,我有一个在教师模式中存储可用天数的问题,在这个应用程序中,教师有他的班级信息,它包括availableDays代表学生可以安排与该教师一起上课的日期和时间,如下所示:

const teacherSchema = new mongoose.Schema({
    classPrice: {
        type: Number,
        required: false,
    },
    education: {
        type: [String],
        required: false,
    },
    degree: {
        type: String,
        required: false,
    },
    availableDays: {
        sunday: {
            type: [String],
            required: false
        },
        monday: {
            type: [String],
            required: false
        },
        tuesday: {
            type: [String],
            required: false
        },
        wednesday: {
            type: [String],
            required: false
        },
        thursday: {
            type: [String],
            required: false
        },
        friday: {
            type: [String],
            required: false
        },
        saturday: {
            type: [String],
            required: false
        },
    },
    subjects: {
        type: [mongoose.Schema.Types.ObjectId],
        ref: 'Subject',
        required: true,
    },
    approved: {
        type: Boolean,
        default: false
    },
    rating: {
        type: Number,
        default: 10
    }
});

那是 store 的“坏”解决方案availableDays。保存时间表的字符串数组。这个想法是像这样保存这些数据:

sunday: ["08:00", "09:00", "10:00"],
monday: ["11:00", "12:00", "13:00"],
...

我在整个结构中面临一些问题。

  1. 当学生选择一天时,例如:(2020/03/02) - (AAAA/DD/MM)。它代表星期三,但在我的模式中,(2020/03/02)和(2020/10/02)之间没有区别。

  2. 当学生选择一天和一个小时来安排他的课程时。此时间戳应该对其他学生不可用,并且不会发生。

我还有其他模式来指定类:

const classSchema = mongoose.Schema({
    teacherId:{
        type: String,
        required: true,
    },
    userId: {
        type: String,
        required: true,
    },
    price: {
        type: Number,
        required: true,
    },
****
    time:{
        type: Number,
        required: true,
    },
    day:{
        tipe: String,
        required: true,
    },
*****
    status:{
        accepted: {
            type: Boolean,
            default: false,
        },
        payment: {
            type: Boolean,
            default: false,
        },
        available: {
            type: Boolean,
            default: false,
        }
    }
});

是这个 Schema,daytime学生从 AvailableDays at Teacher Schema 中选择的 DAY 和 HOUR。

我正在寻找一些解决方案,允许教师选择他自己的日期和时间来安排。教师可以在个人资料编辑页面更新这些日期和时间。

自从我开始这个项目以来,一切都可以改变,我感谢任何帮助。

全面了解这个问题。在前端,我们将日期/时间信息保存在教师注册页面,如下所示:Screenshot1

学生选择是这样的:Screenshot2

标签: javascriptnode.jsmongodb

解决方案


一个月后,我找到了解决方案。首先,我在 Mongo 上创建了一个名为“Agenda”的新模式。该议程有:

{
 teacherId: mongoose.Schema.Types.ObjectId,
 date: Date(),
 availability: true
}

以这种方式,我的老师创建了许多“议程”,它们代表了具有独特时间的不同时间表。就是这样


推荐阅读