Wednesday, 10 May 2017

pushing value to deep nested array in mongodb with mongoose

I have a document that needs to be update on hours in a day and each day object is in the array for the week, the watchers array would need to have values added to it, this is my data structure

 {
"_id" : ObjectId("591356d722cca31f865fb185"),
"uid" : "60ea12b8-bc18-42b6-a79c-e844862b176f",
"mobilitySchedule" : [ 
    {
        "day" : "01",
        "_id" : ObjectId("591356d722cca31f865fb21c"),
        "hours" : [ 
            {
                "hour" : "0000",
                "_id" : ObjectId("591356d722cca31f865fb234"),
                "wathcers" : []
            },
            ...,
            ...,
        ]
      },
      ...,
      ...,
   ],
}

my node service with monogo looks like this, the code runs with no errors but obviously does not really save anything

exports.updateHour = function (updateHours, callback, errback) { console.log(updateHours)

mobilityModel.update({
        'uid': updateHours.uid,
        'mobilitySchedule.day': updateHours.day,
        'mobilitySchedule.hours.hour': updateHours.hours
    },
    {
        "$push": {"mobilitySchedule.$.hours.watchers.": updateHours.gaUid}
    },
    function (err, doc) {
        if (err) {
            logger.logError('mobilityMonitorService - updateHour');
            logger.logError(err);
            errback(err)
            return
        }
        console.log('here is the  doc', doc);
        callback(doc)
    }
  );

};

I essentially need to be able to add and check if the value I am trying to push into array(no duplicates). Any help would be great



via pwborodich

No comments:

Post a Comment