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

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

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 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