【问题标题】:How can i design Schema for below product using mongoose?如何使用猫鼬为以下产品设计架构?
【发布时间】:2017-11-26 16:18:21
【问题描述】:
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"
                    }
                ]
            }

        ]
    }
]

以上是我的产品设计示例,我想为此创建架构。如何为以上产品创建架构。

如何修改我的架构设计

'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)的模型(斜纹布、牛仔布、多臂),颜色基于颜色(红色、白色)选择,我还需要更改基于所选颜色的衬衫图像 有人给点建议吗?帮助我前进

【问题讨论】:

  • 对于数组你使用 [type] 像 [String]

标签: node.js mongodb mongoose


【解决方案1】:

//架构定义示例

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

【讨论】:

  • 能否提供示例文档
  • 我想知道如何为此插入对象
  • //在您的模块中定义您要使用架构的架构路径
  • 如果我制作上述架构,产品将如何存储在集合中
  • @itsme 我已经通过存储在数据库中来更新我的答案
【解决方案2】:

@Ratan Uday Kumar 的回答是对的,但另一种但类似的方式是:

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 个字段的对象没有严格要求。即使字段更少。

【讨论】:

  • 如果我制作上述架构,产品将如何存储?
  • 正是您保存它们的方式!有这些领域或者没有这些领域,完全由你选择!因为它不严格!
  • *他们(上面commnet中的一个错误)
【解决方案3】:
//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. 静态存储

//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.) 动态存储/来自控制器

//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");
        }
    })
});

【讨论】:

  • 提问者要求的更新答案
  • 需要更多帮助
  • @itsme 我该如何帮助你
猜你喜欢
  • 2017-02-05
  • 1970-01-01
  • 2020-01-28
  • 1970-01-01
  • 1970-01-01
  • 2019-08-07
  • 2015-05-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多