我正在开发一个个人商店应用程序,用户可以相互出售商品,但我很难弄清楚如何管理这些产品。例如,如果你想出售一件t恤,你应该能够选择一个大小和颜色等,但如果你出售一台电脑,你应该指定年份,cpu的能力等。所有的产品都有一个标题,价格,图像等,但你如何通过不同的属性?我使用mongodb作为对象。
我正在考虑有一个字段attributes
,它应该是一个具有不同细节的对象,然后是一个包含不同细节的字段type
,它将定义存在的属性。如果是type = 'Computer
,那么我就知道attributes
会像这样。
attributes: {
capacity: 1000 // gb
ram: 4096 // MB
}
等。
在一个普通的面向对象的设计中,我会通过继承/接口来做到这一点。如果您对猫鼬/ node.js中的最佳方法有任何想法,我会很高兴听到的。
如果我在这个问题上没有说清楚,请告诉我什么是模糊的,什么是应该澄清的
编辑:
下面的文章描述了http://learnmongodbthehardway.com/schema/chapter8/问题的一种解决方案
然而,它并没有说明将属性放在哪里。一种解决方案可能是将其存储在类别本身中,但我不确定这里的最佳实践。
发布于 2015-11-17 16:40:53
向Mongoose模式添加继承的一种简单方法是使用Discriminators。这将允许您创建一个父架构,它可以存储跨越所有产品的属性,如标题、价格和图像。然后,您可以创建子模式,该模式将包含特定于产品类型(如电子产品和服装)的属性。例如,在电子架构中,您可以添加cpu和ram的属性,这些属性不会出现在服装架构中。
下面是一个基本的例子,说明我是如何使用Node和Mongoose设置的。
Node/Javascript
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
// When you create a Electronic Product, it will set the type to Eletronic.
var options = { discriminatorKey: 'type' };
// parent Product schema.
var productSchema = new mongoose.Schema({ name: String, price: Number }, options);
var Product = mongoose.model('Product', productSchema);
// child Electronic schema.
var ElectronicProduct = Product.discriminator('Electronic', new mongoose.Schema({ cpu: Number }, options));
var computer = new ElectronicProduct({ name: 'computer', price: 100, cpu: 5 });
computer.save();
// child Clothing schema.
var ClothingProduct = Product.discriminator('Clothing', new mongoose.Schema({ size: String }, options));
var shirt = new ClothingProduct({ name: 'shirt', price: 50, size: 'Small' });
shirt.save();
如果您记录保存的对象,它们应该如下所示
{ _id: 564b55983e5eec1ce2a44038,
type: 'Electronic',
cpu: 5,
price: 100,
name: 'computer' }
{ _id: 564b55983e5eec1ce2a44039,
type: 'Clothing',
size: 'Small',
price: 50,
name: 'shirt' }
当您试图访问产品架构中不存在的属性时,最好在尝试访问属性之前检查该属性是否存在。
发布于 2015-11-17 03:36:07
如果您要问的是如何处理模式,我会说,让您的模型属性成为一个对象数组或混合类型。
http://mongoosejs.com/docs/schematypes.html
因为您可以为属性提供许多可能的选项,所以不可能预先知道所有这些选项。也许您希望用户定义属性的名称(键)和值。然后,一旦得到一个对象,就可以使用一个for in
循环来使用键作为标签和值。
示例:
var attributes = [];
for ( attr in item.attributes ) {
//do something with the key=>value pair
attributes.push({
label: attr,
value: item.attributes[attr]
});
}
//iterate through the new array to display your item attributes
发布于 2015-11-22 10:06:58
对于这个动态数据场景,我更愿意在Mongoose 中将属性保留为空对象,这样您就可以对每个文档使用不同的属性。
模型
var mongoose = require('mongoose');
Schema = mongoose.Schema;
var productSchema = new mongoose.Schema({
title:{type:String,required}, // Common Attributes
price:{type:Number,required}, // Common Attributes
images:[buffer], // Common Attributes
//Other Common Attributes goes here
attributes:{} // Empty attributes object which can vary for each product.
});
var Product = mongoose.model('Product', productSchema);
您可以使用attributes对象为特定产品指定不同的属性。
控制器
var computer = new Product({
title: 'Computer',
price: 1000,
attributes:{
year:2000,
cpu:'test',
power:'75 RMS'
// Other Attributes goes here.
}
});
var shirt = new Product({
title: 'T-Shirt',
price: 999,
attributes:{
size:30,
color:'black'
// Other Attributes goes here.
}
});
参考
http://mongoosejs.com/docs/schematypes.html (查找混合数据类型)
Note
创建此模式还有其他方法,一种方法是将属性作为嵌入文档,并将所有属性组合在一起,如果不适用,可以将值保持为null。
希望这能有所帮助。
https://stackoverflow.com/questions/33695269
复制相似问题