如何使用mongoose(node.js)定义产品系统的不同属性

我正在构build一个个人商店应用程序,用户可以在这里互相销售产品,但是我很难搞清楚如何pipe理这些产品。 例如,如果你想卖T恤,你应该可以select一个尺寸和颜色等,但如果你卖电脑,你应该指定的年份,CPU电源等所有产品有一个标题,价格,图像等上,但你将如何获得与不同的属性? 我正在使用mongodb的对象。

我正在考虑有一个字段attributes应该是一个对象与不同的细节,然后有一个字段type ,将定义哪些属性存在。 如果type = 'Computer那么我会知道, attributes会看起来像这样。

 attributes: { capacity: 1000 // gb ram: 4096 // MB } 

等等

在一个正常的面向对象的devise中,我会通过inheritance/接口来完成这个工作。 如果您对mongoose / node.js中的最佳方法有任何想法,我会很乐意听到。

如果我在问题中没有说清楚,请告诉我什么是模糊的,什么应该澄清

编辑:

下面的文章描述了一个解决schemehttp://learnmongodbthehardway.com/schema/chapter8/

但是,它并没有说明把属性放在哪里。 一种解决scheme可能是将其存储在类别本身,但我不确定这里的最佳做法。

将inheritance添加到Mongoose Schema的简单方法是使用判别器 。 这将允许您创build一个可以存储所有产品(如标题,价格和图像)属性的父级架构。 然后,您可以创build包含特定于产品types(如Electronics and Clothing)的属性的子模式。 例如,在电子模式中,您可以添加cpu和ram的属性,这些属性不会出现在服装模式中。

这里是我如何使用Node和Mongoose进行设置的基本示例。

节点/ 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(); 

如果您logging保存的对象,它们应该看起来像

 { _id: 564b55983e5eec1ce2a44038, type: 'Electronic', cpu: 5, price: 100, name: 'computer' } { _id: 564b55983e5eec1ce2a44039, type: 'Clothing', size: 'Small', price: 50, name: 'shirt' } 

当您尝试访问不在产品模式中的属性时,在尝试访问该属性之前检查该属性是否存在是一种好的做法。

如果你问的是如何接近模式,我会说你的模型属性的对象数组或混合types

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 

对于这个dynamic数据场景,我更愿意将属性保留为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 (查找混合数据types)

注意

还有其他的方法可以创build这个模式,一种方法是将属性作为embedded文档和所有的属性组合,如果不适用,可以保留为空值。

希望这可以帮助。