Mongoose Sub Document Woes

后端 未结 1 468
攒了一身酷
攒了一身酷 2021-01-28 23:20

I have sub document as in metrics. But I don\'t think I am saving correctly, because each document doesn\'t have metrics data correctly..instead it shows metrics: [ \'[object Ob

相关标签:
1条回答
  • 2021-01-28 23:55

    When including a field named type in a sub-doc of a Mongoose schema, you need to define its type using an object or it looks to Mongoose like you're declaring the type of the parent field.

    So change your schema to:

    var CpuSchema = new Schema({
        timeStamp : { type : Date, index: true },
        avaiable : Boolean,
        status : String,
        metrics : [ { duration : String,
            data : Number,
            type : {type: String},
            unit : String
        } ,
        { duration : String,
            data : Number,
            type : {type: String},
            unit : String
        },
        { duration : String, 
            data : Number,
            type : {type: String},
            unit : String
        }
        ]
    });
    
    0 讨论(0)
提交回复
热议问题