简体   繁体   English

Mongoose动态架构字段

[英]Mongoose dynamic schema field

I need to develop a model using mogoose with a field that will hold my object attributes. 我需要使用mogoose开发一个模型,该模型将保存我的对象属性。 My problem is that these attributes are totaly changable, something like: 我的问题是这些属性是完全可变的,如:

StockItem1 : {
    sku: 23492349,
    class: 'computer',
    subclass: 'printer',
    name: 'Hp Laserjet XXX',
    qty: 120,
    attr: {
        laser: true,
        speed: 1200,
        color: white
    }
}

StockItem2 : {
    sku: 22342349,
    class: 'homeappliance',
    subclass: 'refrigerator',
    name: 'GE Refrigerator',
    qty: 23,
    attr: {
        stainlessstell: true,
        doors: 2,
        frostfree: true
    }
}

The attr attributes fields are totally different depending of what type of class/subclass it belongs to. attr属性字段完全不同,具体取决于它所属的class/subclass类型。

What type should be given to attr field in mongoose ? 在猫鼬中应该给attr字段什么类型? I need to filter those in the future, like get all itens where attr.doors == 2 . 我需要在将来过滤那些,比如在attr.doors == 2得到所有的attr.doors == 2

Thanks for helping. 谢谢你的帮助。

Use a Mixed Schema Type. 使用混合模式类型。 Here are the docs . 这是文档 Mixed SchemaTypes are sort of an 'anything goes' type of deal. 混合SchemaTypes是一种“任何事情”类型的交易。 You have flexibility when it comes to defining data but it makes your collection harder to maintain. 在定义数据时,您具有灵活性,但这会使您的集合难以维护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM