Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

194
Views
MongoDB (Mongoose) database structure question

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?
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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 );
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!