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

278
Vistas
How to add a model inside another model with mongoose and node js

I am working on a project and am trying to add a model inside another model but i can't seem to figure out how to do it. Have any suggestions? `

const userSchema = new mongoose.Schema({
    first_name: String,
    last_name: String,
    email: String,
    password: String,
});

const ItemSchema = new mongoose.Schema({
    date: String,
    loc: String,
    title: String,
   passage: String,
   file: String
});

const User = mongoose.model("User", userSchema);
const Item = mongoose.model("Item", ItemSchema);

const user1 = new User({
    first_name: "John",
    last_name: "arbuckle",
    email: "blablabal@asdf",
    password: "123456789"
});

const item1 = new Item({
    date: "Today",
    loc: "here",
    title: "message",
    passage: "This is an example",
    file: "none"
});

I am wanting to add this model const Item = mongoose.model("Item", ItemSchema); to the User model.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You are looking for population.

In userSchema add items : { type: [Schema.Types.ObjectId], ref: 'Item' }

const userSchema = new mongoose.Schema({
    first_name: String,
    last_name: String,
    email: String,
    password: String,
    items : {
         type: [Schema.Types.ObjectId],
         ref: 'Item'
    }
});

const ItemSchema = new mongoose.Schema({
    date: String,
    loc: String,
    title: String,
   passage: String,
   file: String
});
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