在猫鼬模型中存储我已经向其发送消息的潜在教师的 ID 和数据

Storing the id and data of potential teachers to whom I have already sent a message in the mongoose model

我在猫鼬模型中应用正确的方法时遇到问题。案例:在 student应用中还没有选择teachers。想向潜在的老师发送消息。通过转到潜在教师列表,我可以向他们发送私人消息。在 'student' 模型中,我可以添加 属性 'messagesWith' 并仅将与我一起写作的人的 'id' 放入数组中吗?继续聊天,我想查看我已经向其发送消息的人员列表。在哪里存储信息以及我已经向谁发送了消息的数据(或只是 ID)。

用户、教师、学生模型

const userSchema = new Schema({
    name: {
        type: String,
        trim: true,
        required: true,
        maxLength: 32
    },
    surname: {
        type: String,
        trim: true,
        required: true,
        maxLength: 32
    },
    email: {
        type: String,
        unique: true,
        trim: true,
        required: true,
        lowercase: true
    },
    initials: String,
    hashed_password: {
        type: String,
        required: true
    },
    salt: String,
    role: {
        type: String
    },
    profilePicture: {
        data: String,
        default: ''
    },
    cloudinary_id: {
        type: String,
    },
    resetPasswordLink: {
        data: String,
        default: ''
    }
}, {timestamps: true});


const studentSchema = userSchema.clone();
studentSchema.add({
    messagesWith: [],
    teachers: []
});

const teacherSchema = userSchema.clone();
teacherSchema.add({
    isActiveTutor: {
        type: Boolean,
        default: false
    },
  
    teachingLanguage:{
        type: Object
    },
    youtubeUrlId: {
        type: String,
        default: ''
    }
});

消息模型

const messageSchema = new Schema({
    sender: {
        type: Schema.ObjectId
    },
    receiver: {
        type: Schema.ObjectId
    },
    msg: {type: String},
    viewed: {type: Boolean, default: false},
    createdAt: {
        type: Date,
        default: Date.now
    }
});

有很多方法可以实现模型来解决您的问题...

有些人把Nosql数据库当作reliatinal数据库使用,这两种方式都依赖于你现在的requirments.Your要求很低,两种方法都可以轻松使用。

您需要预测您的报告类型,以便在 future.Because 大数据量、填充和加入聚合集合时不会出现问题,速度会变慢。

尽可能避免链接集合以通过查找获得所需的报告(尤其是在像 _id 这样的索引字段上)

我今晚在 Github 创建了一个项目,你可以查看 messaging project 并为其编写了 4 个 API,基于两个没有关系的模型(学生模型 <--> 教师模型)并添加邮递员文件夹中的邮递员,(创建学生,创建教师,发送消息,通过学生ID获取学生的消息接收者)

你可以运行,审查和测试项目,如果我有空闲时间,我会根​​据关系数据(学生模型<-->消息模型<-->教师模型)

学生资料

{
    "_id" : ObjectId("5ffb8889905c523b70ec2977"),
    "name" : "st1",
    "surname" : "lastname1",
    "email" : "email1@test.com",
    "hashed_password" : "123",
    "role" : "user",
    "messages" : [ 
        {
            "contentInfo" : {
                "viewed" : false,
                "msg" : "from student to a teacher from/studentId/to/teacherId",
                "createdAt" : ISODate("2021-01-10T23:20:34.796Z")
            },
            "_id" : ObjectId("5ffb8bc223286f4934f2a013"),
            "receiver" : ObjectId("5ffb6d4934cf83268096b97f")
        }, 
        {
            "contentInfo" : {
                "viewed" : false,
                "msg" : "from student to a teacher from/studentId/to/teacherId",
                "createdAt" : ISODate("2021-01-10T23:21:33.765Z")
            },
            "_id" : ObjectId("5ffb8bfd23286f4934f2a016"),
            "receiver" : ObjectId("5ffb8beb23286f4934f2a015")
        }, 
        {
            "contentInfo" : {
                "viewed" : false,
                "msg" : "from student to a teacher from/studentId/to/teacherId",
                "createdAt" : ISODate("2021-01-10T23:25:43.099Z")
            },
            "_id" : ObjectId("5ffb8cf7641f2e2e78686961"),
            "receiver" : ObjectId("5ffb8cea641f2e2e78686960")
        }
    ],
    "createdAt" : ISODate("2021-01-10T23:06:49.878Z"),
    "updatedAt" : ISODate("2021-01-10T23:25:43.112Z"),
    "__v" : 3
}

教师资料

/* 1 */
{
    "_id" : ObjectId("5ffb8beb23286f4934f2a015"),
    "isActiveTutor" : false,
    "youtubeUrlId" : "",
    "name" : "t1",
    "messages" : [ 
        {
            "contentInfo" : {
                "viewed" : false,
                "msg" : "from student to a teacher from/studentId/to/teacherId",
                "createdAt" : ISODate("2021-01-10T23:21:33.784Z")
            },
            "_id" : ObjectId("5ffb8bfd23286f4934f2a017"),
            "sender" : ObjectId("5ffb8889905c523b70ec2977")
        }
    ],
    "__v" : 1
}

/* 2 */
{
    "_id" : ObjectId("5ffb8cea641f2e2e78686960"),
    "isActiveTutor" : false,
    "youtubeUrlId" : "",
    "name" : "t2",
    "messages" : [ 
        {
            "contentInfo" : {
                "viewed" : false,
                "msg" : "from student to a teacher from/studentId/to/teacherId",
                "createdAt" : ISODate("2021-01-10T23:25:43.124Z")
            },
            "_id" : ObjectId("5ffb8cf7641f2e2e78686962"),
            "sender" : ObjectId("5ffb8889905c523b70ec2977")
        }
    ],
    "__v" : 1
}