Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

261
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!