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

255
Vistas
Mongoose model with variations

I'm trying to make a product model which has multi variants and this variants has multi packages any each package has different price and img for example:

Product Ram and CPU

Each one has a different variants, for eg: for Ram there is 16gb which is of price 50$ and 32gb which is of price 100%, for CPU there are i7 and i5 and each one of those has different price, how can i create a model for the scenario.

const ProductSchema = new Schema({
  img:{ type:String, required:true },
  name: [{
    name:String,
  }],
  variants:[{
      name:String,
      target:[{
        name:String,
        img:String
      }],
      package:[{
          name:String,
          price:Number
      }]
  }]
})

I try to do this but it didn't worked

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can just create a model out of the schema and push to the package array the relevant data:

const ProductModel = new model('Product', ProductSchema);
...
// name, and variants are arrays so they default to empty arrays, only 'img' is required
const newProduct = new ProductModel({
   img: 'http://balhblahimg.com/'
};

const variant = {
  name: 'foo',
  target: [],
  package: []
};

...
...
variant.target.push({name: 'a', img: 'b'});
variant.package.push({name: 'i7', price: 54});

// Do manipulation on variant object, add to pacage and target...
// and push it to the newProduct
newProduct.name.push('boo');
newProduct.variants.push(variant);

newProduct.save();
about 4 years ago · Juan Pablo Isaza 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