本文介绍了猫鼬子文档问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 Object]', '[object Object]', '[object Object]' ] and the data is impossible to access. This is really difficult to figure out since Mongoose doesn't give errors for this kind of stuff. If anyone could please tell me what is wrong.

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

To make things more confusing, in the browser it shows that metrics has 3 arrays:

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;


这是我的保存功能:


Here is my save function:

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


这是我要插入的数据:


Here is my data that gets inserted:

{ 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您要声明父字段的类型.

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

这篇关于猫鼬子文档问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 18:47
查看更多