Search code examples
javascriptmongodbmongoosemongoose-schema

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


I'm having trouble applying the right approach in the mongoose model. Case: There are no teachers selected yet in the student application. Would like to send messages to potential teachers. By going to the list of potential teachers, I can send them a private message. In the 'student' model, can I add the property 'messagesWith' and put only the 'id' of people I am writing with in the array? Moving on to the chat, I would like to view a list of people to whom I have already sent a message. Where to store information and what data (or just id) to whom I have already sent a message.

User, teacher, student model

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: ''
    }
});

message model

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

Solution

  • there are many ways to implement models to resolve your problem...

    Some people use the Nosql database like a reliatinal database,both of them on depend your requirments.Your requirments now are very low and both methods can be used easily.

    You need to anticipate your types of reports so that you do not have problems in the future.Because for a large data volume, populate and join collection with aggregation, slows down.

    Avoid linking Collection as much as possible to get the report you want with a find(especially on Indexed fields likes _id)

    I created a project at Github tonight you can check the messaging project and wrote 4 apis for it, based on two model without reliational (student Model <--> teacher Model)and added postman in postman follder,(create student, create teacher, send message, get message reciervers of a student by student id)

    you can run,review and test the project, if I have a free time I worte based on reliational data (student model<-->message model<-->teacher model)

    student data

    {
        "_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
    }
    

    teacher data

    /* 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
    }