具有不同变体模式的产品

时间:2018-07-16 12:47:43

标签: mongodb mongoose database-design nosql schema

我正在尝试使用MongoDB创建一个电子商务网站。我已经创建了一个产品和变量模型,我的问题是如何搜索带有变量的产品,例如对于“尺寸”用户可以将变量值添加为“ S”或“小”。在这种情况下,如何搜索具有较小产品的产品,因为该产品具有多种变体,如何列出例如。所有产品体积小。这是我的变形模型。

var variantSchema = Schema({
name: {
    type: String,
    required: true,
    unique: true
},
count: {type: Number, default : 0}
});

我的产品架构是:

var productSchema = Schema({
sku: {
    type: String,
    lowercase: true
}, //, required: true, unique: true
name: {
    type: String,
    lowercase: true,
    max: 65,
    required: true
},
slug: {
    type: String,
    lowercase: true,
    unique: true,
    index: true,
    slug: "name",
    slug_padding_size: 3
},
status: Boolean,
listPrice: Number,
description: {
    short: {
        type: String,
        trim: true,
        lowercase: true
    },
    long: {
        type: String,
        trim: true,
        lowercase: true
    }
},
images: [],
categoryId: {
    type: Schema.Types.ObjectId,
    ref: 'Category'
},
userId: {
    type: Schema.Types.ObjectId,
    ref: 'User',
    required: true
},
createdAt: {
    type: Date,
    default: Date.now
},
updatedAt: {
    type: Date,
    default: Date.now
},
isActive: Boolean,
vars: [
    {
        varId : {
            type: Schema.Types.ObjectId,
            ref: 'Variants'
        },
        values: [
            {
                value : String,
                image:[]
            }
        ]
    }
]
});

1 个答案:

答案 0 :(得分:0)

根据您的评论。

您可以忽略大小写来区分“小”和“小”。

UserModel.findOne({
    email: {'$regex': "^"+ email +"$", $options:'i'}
}, function (err, data) {
    callback(err, data)
});

但是您不能将SSmall匹配。

方法1:

您需要保留可能要考虑为Small的单词。也许是通过在Variant Schema中插入这样的数组["S", "Small"]来实现的。但是在这种情况下。您必须注意SS可以是任何东西。 (不建议您使用这种方法)

方法2:

我建议创建一个可以显示大小的模式(SizeSchema)。例如Small, Large, Extra small, Extra Large etc...并将SizeSchema引用到VariantSchema,将ProductSchema引用到VariantSchema。 (三重关系)。而这对于最终用户将是固定的。没有人会像“ S”这样的选项。

希望这对您有所帮助。

相关问题