简体   繁体   中英

Mongoose dynamic schema field

I need to develop a model using mogoose with a field that will hold my object attributes. 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.

What type should be given to attr field in mongoose ? I need to filter those in the future, like get all itens where 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. You have flexibility when it comes to defining data but it makes your collection harder to maintain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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