Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

196
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda