繁体   English   中英

猫鼬子文档问题

[英]Mongoose Sub Document Woes

我有子文档,如度量。 但是我认为我没有正确保存,因为每个文档没有正确的指标数据。相反,它显示了指标:['[object Object]','[object Object]','[object Object]']并且无法访问数据。 这真的很难弄清楚,因为猫鼬不会给这类东西带来错误。 如果有人可以告诉我哪里出了问题。

编辑:为了使事情更加混乱,在浏览器中它显示指标具有3个数组:

Object {cpuResult: Object}
cpuResult: Object
__v: 0
_id: "53b781d9fb272c4f44d8d1d8"
avaiable: true
metrics: Array[3]
0: "[object Object]"
1: "[object Object]"
2: "[object Object]"
length: 3

这是我的架构:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var CpuSchema = new Schema({
    timeStamp : { type : Date, index: true },
    avaiable : Boolean,
    status : String,
    metrics : [ { duration : String,
        data : Number,
        type : String,
        unit : String
    } ,
    { duration : String,
        data : Number,
        type : String,
        unit : String
    },
    { duration : String, 
        data : Number,
        type : String,
        unit : String
    }
    ]
});

module.exports = CpuSchema;

这是我的保存功能:

function saveCpu(cpuResult) {
    var cpu = new Cpu ({
        timeStamp : cpuResult.timestamp,
        avaiable : cpuResult.available,
        status : cpuResult.status,
        metrics : [ { duration : "15m",
                      data : cpuResult.metrics["15m"].data,
                      type: cpuResult.metrics["15m"].type,
                      unit: cpuResult.metrics["15m"].unit
                    },
                    { duration : "5m",
                        data : cpuResult.metrics["5m"].data,
                        type: cpuResult.metrics["5m"].type,
                        unit: cpuResult.metrics["5m"].unit
                    },
                    { duration : "1m",
                        data : cpuResult.metrics["1m"].data,
                        type: cpuResult.metrics["1m"].type,
                        unit: cpuResult.metrics["1m"].unit
                    }]
    });
    cpu.save(function (err, product, numberAffected) { 
        db_finish(err, product, numberAffected, 
                  cpuResult, "cpuResult") });

这是我插入的数据:

{ timestamp: 1404534588528,
  available: true,
  status: 'success',
  metrics: 
   { '15m': { data: 0.05, type: 'n', unit: 'unknown' },
     '5m': { data: 0.01, type: 'n', unit: 'unknown' },
     '1m': { data: 0, type: 'n', unit: 'unknown' } } }

当在Mongoose模式的子文档中包含一个名为type的字段时,您需要使用一个对象定义其类型,或者看起来像在声明父字段的类型一样,它看起来像Mongoose。

因此,将您的架构更改为:

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
    }
    ]
});

暂无
暂无

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

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