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

208
Vistas
Mongoose many to many relation

I am beginner Node.JS and Mongoose programmer. So far I have created a very simple blog with 3 Models - Article, User, Categories and everything is working fine. Now I want to create comments. So I want every user to be able to have manny comments and every articles also to be able to have manny comments. How can I do that and how can I represent logic in the controller to interact with each other:

let articleSchema = mongoose.Schema (
{
    author: {type: ObjectId, ref: 'User'},
    title: {type: String, required: true },
    content: {type: String, required: true },
    category: {type: ObjectId, ref: 'Category', required: true},
    date: {type: Date, default: Date.now() },
}
);

let userSchema = mongoose.Schema(
{
    email: {type: String, required: true, unique: true},
    passwordHash: {type: String, required: true},
    fullName: {type: String, required: true},
    salt: {type: String, required: true},
    articles: [{type: ObjectId, ref: 'Article'}],
    roles: [{type: ObjectId, ref: 'Role'}]
}
);

let categorySchema = mongoose.Schema (
{
    name: {type: String, required:true},
    articles: [{type: ObjectId, ref: 'Article'}]
}
); 
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Create Comment schema which will reference Author and Article:

let commenSchema = mongoose.Schema (
{
    author: {type: ObjectId, ref: 'User', required: true},
    content: {type: String, required: true },
    article: {type: ObjectId, ref: 'Article', required: true},
    date: {type: Date, default: Date.now() },
});
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