2016-08-07 123 views
0

我有一个时间序列数据的集合。流星汇集更新子文档

这样的:

{ _id: 'queBoS2mgjovC85uY', 
    sum_samples: 0, 
    type: 'Temperature', 
    timestamp_minute: "Sun Aug 07 2016 10:03:18 GMT-0400 (EDT)", 
    num_samples: 0, 
    Sensor:{ 
     '0': { value: 0, real: false }, 
     '1': { value: 0, real: false }, 
     '2': { value: 0, real: false }, 
     '3': { value: 0, real: false }, 
     '4': { value: 0, real: false }, 
     '5': { value: 0, real: false }, 
     '6': { value: 0, real: false }, 
     '7': { value: 0, real: false }, 
     '8': { value: 0, real: false }, 
     '9': { value: 0, real: false }, 
     '10': { value: 0, real: false }, 
     ... 
     '59': { value: 0, real: false } 

    } 
} 

在 '0', '1' 对象是在一个小时分钟。 现在我想更新一个分钟值,我已经测量并将“真实”设置为真,真实值将指示之前的值是实际测量的传感器值而不是初始值。

但所有的Temperature.update()我试过了不起作用。 也许是因为我对流星和mongodb知之甚少,因为我是一个业余爱好程序员。 我总是想更新最新的条目。 有人可以帮我吗? 也许有一个链接,我可以阅读背后的基础知识。

谢谢。 迈克尔

附:

我的代码是目前prety简单。

if (Meteor.isServer) { 
    Temperatures.insert({sum_samples: 0, type: "Temperatures"}); 
    var test = Temperatures.findOne({}, {sort: {timestamp_minute: -1}}); 

    Temperatures.update({_id: test._id},{$set: {"Sensor.32.value": 2032, "Sensor.32.real": true} }); 

} 

简单架构我也这么做。

Schemas = {}; 

Temperatures = new Meteor.Collection("Temperatures"); 

Schemas.seriesData = new SimpleSchema({ 
    timestamp_minute: { 
     type: Date, 
     defaultValue: new Date 
     }, 
    num_samples: { 
     type: Number, 
     defaultValue: 0 
    }, 
    sum_samples: { 
     type: Number, 
     defaultValue: 0 
    }, 
    type: { 
     type: String 
    }, 
    Sensor: { 
     type: Object, 
     blackbox: true, 
     optional: true, 
     autoValue: function(){ 
      if (this.operator === null && !this.isSet){ 
       var object = {}; 
       for(var i=0; i<=59;i++){ 
        object[i]={value: 0, real: false}; 
       } 
       return object; 
      } 
     } 

    } 
}); 
Temperatures.attachSchema(Schemas.seriesData); 
+0

先分享你的代码,我们可以帮您 –

+0

我已经添加了代码。目前它非常简单,因为我只测试代码。希望这可以帮助你。 – Michael

回答

0

难道是单引号的整数?

Temperatures.update({_id: test._id},{$set: {"Sensor.'32'.value": 2032, "Sensor.'32'.real": true} }); 

我还可以建议Sensor是一个数组吗?喜欢:

Sensor [ 
    { ... }, 
    { ... } 
] 
+0

感谢您的反馈。当我使用您的更新代码时,我的更新无法按预期工作。我会得到这个新字段[code]'\ '32 \'':{value:2032,real:true} [/ code]是一个数组。更容易访问?数组的优点是什么?你会怎么做? – Michael

+0

以及我如何更新它们?与Temperatures.update({_ id:test._id},{$ set:{“Sensor。[32] .value”:2032,“Sensor。[32] .real”:true}});它不工作。 – Michael

+0

@Michael Syntax将会是''Sensor.32。'。此外,你可以使用'''反引号来使你的评论格式成为monotype,所以\'一些代码\'变成'一些代码'(我躲过了那些反引号通过\\'让它出现在那里,因此为什么没有成为代码) –