name: aaaa shirts
category: shirts
subcategory: [{
type: slimline,
model: [{
\"type\": \"twill\",
\"colour\": [{
@Ratan Uday Kumar's answer is right, but another but similar way is:
var ProductSchema = new mongoose.Schema({
category :{type:String},
name :{type:String},
description :{type:String},
type :[{type:String}],
fabric :[{type:String}],
size :[{type:Number}],
color :[{type:String}],
created_at :{ type: Date },
updated_at :{ type: Date, default: Date.now },
updated: {type: Date, default: Date.now}
}, { versionKey: false }, {strict: false});
export default mongoose.model('Product', ProductSchema);
{strict: false} is for future! How? Now your schema has 10 fields, if in future you want to add an object with 12 (anything more than 10), you can do it, because there is no strictness in inserting objects with these 10 fields. Even if less fields.
//Schema definition Example
var ProductSchema = new mongoose.Schema({
name:String,
category:String,
subcategory:[{
type:String,
model:[{
type:String,
colour:[{
name:String,
image:String
}],
size:[{
val:Number,
price:Number
}]
}]
}],
description:String,
created_at:{ type: Date },
updated_at:{ type: Date, default: Date.now },
updated:{type: Date, default: Date.now}
}, { versionKey: false },{strict: false});
export default mongoose.model('Product', ProductSchema);
//Schema Defination and model.js
var ProductSchema = new mongoose.Schema({
name:String,
category:String,
subcategory:[{
type:String,
model:[{
type:String,
colour:[{
name:String,
image:String
}],
size:[{
val:Number,
price:Number
}]
}]
}],
description:String,
created_at:{ type: Date },
updated_at:{ type: Date, default: Date.now },
updated:{type: Date, default: Date.now}
}, { versionKey: false },{strict: false});
export default mongoose.model('Product', ProductSchema);
Storing Product Details in Database Collection 1. Static Storing
//Static Storing into Database
var ProductSchema = require('path/to/ProductSchema.js');
app.post('/Store_Product_Details',function (req,res) {
var Name = 'Mens Formal Shirts';
var Category = 'Shirts';
var SubCategory = [{
type: "slimline",
model: [{
"type": "twill",
"colour": [{
"name": "red",
"image": "red.jpg"
},
{
"name": "white",
"image": "white.jpg"
}
],
"size": [{
"val": 32,
"price": "1000"
},
{
"val": 24,
"price": "1244"
}
]
}, {
"type": "denim",
"colour": [{
"name": "red",
"image": "red.jpg"
},
{
"name": "white",
"image": "white.jpg"
}
],
"size": [{
"val": 32,
"price": 1000
},
{
"val": 24,
"price": 1244
}
]
}
]
},{
type: "superslim",
model: [{
"type": "denim",
"colour": [{
"name": "red",
"image": "red.jpg"
},{
"name": "white",
"image": "white.jpg"
}
],
"size": [{
"val": 32,
"price": 1000
},{
"val": 24,
"price": 1244
}
]
},{
"type": "dobby",
"colour": [{
"name": "red",
"image": "red.jpg"
},
{
"name": "white",
"image": "white.jpg"
}
],
"size": [{
"val": 32,
"price": 1000
},
{
"val": 24,
"price": 1244
}
]
}
]
}
]
var Description = 'Mens Formal Wear';
var date = new Date();
var ProductData = new ProductSchema({
name:Name,
category:Category,
subcategory:SubCategory,
description:Description,
created_at:date
})
ProductData.save(function (err,Status) {
if(!err){
res.send("Product Stored Successfully");
}else {
res.send("Oops! Something went Wrong");
}
})
});
2.) Dynamic Storing / From Controller
//Dynamically Storing or from Controller
var ProductSchema = require('path/to/ProductSchema.js');
app.post('/Store_Product_Details',function (req,res) {
var Name = req.body.Name;
var Category = req.body.Category;
var SubCategory = req.body.SubCategory;
var Description = req.body.Description;
var date = new Date();
var ProductData = new ProductSchema({
name:Name,
category:Category,
subcategory:SubCategory,
description:Description,
created_at:date
})
ProductData.save(function (err,Status) {
if(!err){
res.send("Product Stored Successfully");
}else {
res.send("Oops! Something went Wrong");
}
})
});