简体   繁体   中英

Structuring My Models with Mongoose/MongoDB

I have begun diving into the server side of things lately, and am working on an app where I need to think about how I plan my models.

My users are teachers, and in the dashboard will have the ability to create a list of students. My schema's will contain more directives to prevent duplicates being created, but I have simplified them here. Here's what I have attempted so far:

// Teacher Model
const Teacher = new Schema({
   fname: String,
   lname: String,
   email: String,
})

// Student Model
const Student = new Schema({
   fname: String,
   lname: String,
   full: String,
   uuid: String
   grades: {
    classwork: Array,
    quizzes: Array,
    tests: Array
   }
})

Here's where my inexperience with backend work comes into play. This setup doesn't really make sense to me. Say when I go and save a student, it will create a new student under the student collection in the database. This is not ideal, as the student should be stored in a way that is strictly accessible to the teacher who created it.

I was thinking about creating a new key in my Teachers Schema called "students"(which would be an array) that would push a student into it each time one was created.

It's definitely important that I plan this properly, as the teacher is going to have much more ability in the future, like creating assignments, grading students etc. I'd like to design this with best practices in mind, to ensure the teachers data is safe from other users.

Using nested array in mongo model is not so smooth. I can suggest to think about size of this array.

If there is a chance that your array can grow - don't use it.

My suggestion for your database design is simple. Add teacherId to the student model. This way, when you need to fetch students list according to the certain teacher - you can easily query by teacherId .

So your student schema modified would look this way:

const Student = new Schema({
   teacherId: {
     type: mongoose.Schema.Types.ObjectId,
     index: true,
     required: true
   },
   fname: String,
   lname: String,
   full: String,
   uuid: String
   grades: {
    classwork: Array,
    quizzes: Array,
    tests: Array
   }
});

I don't agree with @Lazyexpert. MongoDB is a non-relational database and you can store until 16Mb of data per document. It is really enough for what you need

The maximum BSON document size is 16 megabytes. The maximum document size helps ensure that a single document cannot use excessive amount of RAM or, during transmission, excessive amount of bandwidth. To store documents larger than the maximum size, MongoDB provides the GridFS API.

ie: https://docs.mongodb.com/manual/reference/limits/

So I suggest you just add the datas of each student directly in your teacher.

You can find some tips here : https://www.safaribooksonline.com/library/view/50-tips-and/9781449306779/ch01.html

So your model would looks something like that :

const Teacher = new Schema({
   fname: String,
   lname: String,
   email: String,
   students : [
      {
         fname: String,
         lname: String,
         full: String,
         uuid: String
         grades: {
             classwork: Array,
             quizzes: Array,
             tests: Array
         },
      },
   ],
})

And if you absolutely want a collection Student as well, then use a "post" middleware on the "save" action on your student schema. Something like this :

StudentSchema.post('save', function(doc) {
  Teacher.findOneAndUpdate({_id: <your teacher id>}, <your student object>, callback);
});

ie: mongoosejs.com/docs/api.html#schema_Schema-post

Good luck :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM