Node.js 如何使用mongoose为以下产品设计模式?
上面是我的产品设计示例,我想为此创建模式。如何为上述产品创建模式 如何修改模式设计Node.js 如何使用mongoose为以下产品设计模式?,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,上面是我的产品设计示例,我想为此创建模式。如何为上述产品创建模式 如何修改模式设计 name: aaaa shirts category: shirts subcategory: [{ type: slimline, model: [{ "type": "twill", "colour": [{ "name": "red",
name: aaaa shirts
category: shirts
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"
}
]
}
]
}
]
例如,我选择slimline,我需要发送带有图像和尺寸(28,30,32,36)的模型(斜纹布、牛仔布、多臂布),并根据颜色(红色、白色)选择颜色。此外,我还需要根据所选颜色更改衬衫图像
有人提些建议吗?帮助我前进//模式定义示例
'use strict';
import mongoose from 'mongoose';
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var ProductSchema = new mongoose.Schema({
name : String,
category : String,
subcategory : ??
description : String,
created_at : { type: Date },
updated_at : { type: Date, default: Date.now },
updated: {type: Date, default: Date.now}
}, { versionKey: false });
export default mongoose.model('Product', ProductSchema);
@Ratan Uday Kumar的答案是正确的,但另一种类似的方式是:
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);
{strict:false}是为了未来!怎么用?现在,您的模式有10个字段,如果将来您想添加一个包含12个字段的对象(大于10个字段),您可以这样做,因为插入包含这10个字段的对象没有严格要求。即使更少的领域
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);
在数据库集合中存储产品详细信息
1.静态存储
//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);
2.)动态存储/来自控制器
//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");
}
})
});
对于数组u,使用[type]类似于[String]您能给出示例文档吗?我想知道如何插入此对象//在模块中定义您要使用的模式的模式路径。如果我制作上述模式,产品将如何存储在其中collection@itsme我已经更新了我的答案,在数据库中存储,如果我做了上面的模式,产品将如何存储?正是您保存它们的方式!有了这些字段,或者没有这些字段,完全属于你!因为它不严格*他们(上面commnet中的一个错误)又更新了一个问题的答案help@itsme我能帮你什么
//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");
}
})
});