Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

197
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda