mongodbmeteormeteor-collection2simple-schema

Meteor Collection Update Sub Document


I have a collection for time-series Data.

like this:

{ _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 }

    }
}

The '0', '1' Object are Minutes in an Hour. Now i want to update one Minute Value what i have measured and set "real" to true, the real would indicate, that the value before is a real measured sensorvalue and not an initial value.

But all Temperature.update() i've tried doesn't work. Maybe because of my small knowledge about meteor and mongodb because i'm a hobby programmer. I always want to update the newest entry. Can somebody help me out? Maybe with a link where i can read the basics behind.

Thank you. Michael

attached:

My code is prety simple at the moment.

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

}

The simple-Schema i did like that.

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

Solution

  • Could it be the single quotes round the number?

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

    May I also suggest that Sensor be an array? Like:

     Sensor [
         { ... },
         { ... }
     ]