如何使用猫鼬为以下产品设计Schema?

时间:2017-06-23 07:22:31

标签: node.js mongodb mongoose

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"
                    }
                ]
            }

        ]
    }
]

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

如何修改架构设计

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

例如我选择slimline我需要发送模型(斜纹,牛仔布,多臂)图像和尺寸(28,30,32,36)颜色基于颜色(红色,白色)选择我还需要更改基于所选颜色的衬衫图像 任何人给出一些建议?帮助我前进

3 个答案:

答案 0 :(得分: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);

在数据库集合中存储产品详细信息 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");
        }
    })
});

答案 1 :(得分:0)

//架构定义示例

<form>
        <select name="status" id="showStatus" class="form-control" style="width: 125px">
        <option value="">Pilih Status:</option>
        <option value="1">Lulus</option>
        <option value="0">Tidak Lulus</option>
        </select>
        </form>
<script type="text/javascript">
     $("#showStatus").change(function(){
        var status = $('#showStatus').val();
            alert(status);
  $.ajax({
      type:"POST",
      url:'<?php echo base_url("sales/status/") ?>'+status,
      data:"salesstatus="+status,
      dataType:'json',
      success:function(data){
        $("#example1").html(data);
      },
      error:function(XMLHttpRequest){
        alert(XMLHttpRequest.responseText);
      }
  });
});

         </script>

答案 2 :(得分:0)

@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个字段的对象没有严格性。即使字段较少。