首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用mongoose为以下产品设计模式?

如何使用mongoose为以下产品设计模式?
EN

Stack Overflow用户
提问于 2017-06-23 15:22:31
回答 3查看 3.8K关注 0票数 0
代码语言:javascript
复制
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"
                    }
                ]
            }

        ]
    }
]

上面是我的产品设计示例,我想为这个.how创建模式,我可以为上面的产品创建模式。

如何修改我的架构设计

代码语言:javascript
复制
'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);

例如,我选择瘦身我需要发送型号(斜纹斜纹,牛仔,多臂)与图像和尺寸(28,30,32,36)与颜色(红色,白色)的选择也我需要改变衬衫图像根据选择的颜色谁给一些建议?帮我一把继续前进

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-06-29 19:56:51

代码语言:javascript
复制
//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);

在数据库集合中存储产品详细信息1.静态存储

代码语言:javascript
复制
//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.)动态存储/来自控制器

代码语言:javascript
复制
//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");
        }
    })
});
票数 1
EN

Stack Overflow用户

发布于 2017-06-23 15:38:53

//模式定义示例

代码语言:javascript
复制
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);
票数 0
EN

Stack Overflow用户

发布于 2017-06-26 18:52:56

@Ratan Uday Kumar的答案是正确的,但另一种类似的方式是:

代码语言:javascript
复制
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}是为将来准备的!多么?现在您的模式有10个字段,如果将来您想要添加一个包含12个字段(大于10的任何值)的对象,您可以这样做,因为插入包含这10个字段的对象并不严格。即使字段较少。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44715430

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档