Home > Enterprise >  MongoDB (Mongoose) database structure question
MongoDB (Mongoose) database structure question

Time:12-07

I'm currently learning about Mongoose and nodeJS. I have to Create a database in which the registrations for courses and the scores per course of students are saved. Each subject is also linked to a teacher. Think about the structure of the database. Make sure you can argue why you are connecting data via 'embed' or 'reference'. Also think about where you use indices and unique indices.

The way I am currently thinking:

  • A student has multiple courses
  • A course has multiple students
  • A student has multiple scores on one course
  • A course has one teacher

I tried making the following mongoose schemes:

StudentSchema:

const mongoose = require("mongoose");
const studentSchema = new mongoose.Schema({
        name: {
            type: String,
            minLength: 3,
            trim: true,
            lowercase: true,
            validate: {
                validator: (value) => /^[a-z ] $/.test(value),
                message: (props) => `${props.value} is not a valid name!`
            },
            required: [ true, "Name is required" ]
        },
        id: {
            type: Number,
            index: { unique: true },
            min: [ 1, "ID must be greater than 0" ],
            required: [ true, "ID is required" ]
        },
        courses : [ { type: mongoose.Schema.Types.ObjectId, ref: "Course" } ]
    },
    { collection: "students" }
);

module.exports = mongoose.model("Student", studentSchema );

CourseSchema:

const mongoose = require("mongoose");
const courseSchema = new mongoose.Schema({
        title: {
            type: String,
            minLength: 3,
            trim:true,
            lowercase:true,
            validate: {
                validator: (value) => /^[a-z ] $/.test(value),
                message: (props) => `${props.value} is not a valid course title`
            },
            required: [true, "Course title is required"]
        },
        teacher: {
            type: String,
            minLength: 3,
            trim: true,
            lowercase: true,
            validate: {
                validator: (value) => /^[a-z ] $/.test(value),
                message: (props) => `${props.value} is not a valid teacher name`
            },
            required: [true, "teacher is required"]
        },
        student : { type: mongoose.Schema.Types.ObjectId, ref: "Student" }
    },
    { collection: "courses" }
);

module.exports = mongoose.model("Course", courseSchema );

The problems I am currently facing:

  1. In the StudentSchema I am using an Id because I want 1,2,3,4 etc instead of randomly generated id from MongoDB. Is this the correct way to do it?
  2. How do I add scores to the database and where would I place it?

CodePudding user response:

It's really a matter of opinion.

If you need a unique ID per document, you should just let Mongo handle it. It's there for a reason.

In my opinion you would add a scores array in the courses schema.

const mongoose = require("mongoose");
const courseSchema = new mongoose.Schema({
        title: {
            type: String,
            minLength: 3,
            trim:true,
            lowercase:true,
            validate: {
                validator: (value) => /^[a-z ] $/.test(value),
                message: (props) => `${props.value} is not a valid course title`
            },
            required: [true, "Course title is required"]
        },
        teacher: {
            type: String,
            minLength: 3,
            trim: true,
            lowercase: true,
            validate: {
                validator: (value) => /^[a-z ] $/.test(value),
                message: (props) => `${props.value} is not a valid teacher name`
            },
            required: [true, "teacher is required"]
        },
        student : { type: mongoose.Schema.Types.ObjectId, ref: "Student" },
        scores: [{
            score: Number, // the grade
            assignment_id: mongoose.Schema.Types.ObjectId// the id to the test/quiz/homework/etc.. which was graded
        }],
        // You could also just have an array of numbers (scores)
        scores: { type: [Number] }
    },
    { collection: "courses" }
);

module.exports = mongoose.model("Course", courseSchema );
  • Related