Mongoose:具有不同键/值的嵌套模式

时间:2014-07-09 16:13:04

标签: node.js mongodb mongoose nosql

我遇到以下情况:

一个人可以拥有多种产品。这些产品都有自己的配置字段。

我想出了类似的东西:

var UserSchema = new Schema({
    ...
    productsOwned: [
        {
            _id: {type: ObjectId, ref: 'Product'},
            configs: {}
        }
    ]  
})

和产品:

var ProductSchema = new Schema({
    name: {type: String},
    price: {type: Number},
    addedOn: {type: Date},
    description: {type: String},
    configs: {}
});

UserConfig的:

var UserConfigSchema = new Schema({
    product: {type: Schema.ObjectId, ref: 'Product'}
});

我不确定是否必须为配置定义每个产品的另一个架构以及如何相应地填充它们

例如我想要这个:

var userConfig = new UserConfig({
    product: product._id,
    test1: "Hi",
    test2: [{
        server1: "blablbla",
        server2: {
            "blabla1": "hi1",
            "blabla2": "hi3"
        }
    }]
});

var userConfig2 = new UserConfig({
    product: product._id,
    "configkey1": "value1"
});

我现在正在使用:

user.productsOwnedConfigs.addToSet(userConfig);

但它只添加产品密钥

0 个答案:

没有答案