So the problem is this. I'm reading some values from an API and I'm doing the initial insert with each new subscription. However, once I fetch the daily records, I want to match the dates of the already written object within the array so I can replace its values. Currently, all it does is inserting the object values again. I was hoping for an overwrite.
// API logic (within a forEach loop)
// ....
// if there's a new value the current day
if (present_date == item.date){
// update existent via the date
User.update(
{ "object_array" : { "$elemMatch": { "date": present_date } }},
{ $set: { "object_array.$.value": item.value } },
);
} else {
// push new values with new dates
doc.object_array.push({
value: (item.value) ? item.value * .15 : 0,
date: item.date
});
}
...
// outside loop
doc.save();
Any insight? I must be missing something.