This is an overview of the structure of my collection:
{
profile: {
first_name: 'Plop',
surname: 'Plopette',
...
},
medical_history: {
significant_illnesses: [
'Asthma',
'Diabetes'
],
...
}
}
How do I access and update one of the items in the medical_history.significant_illnesses
array?
What I have is failing miserably:
Patients.update(Session.get("current_patient"), {
$push: {
"medical_history.significant_surgeries.surgeryIndex": $(event.target).val().trim()
}
});
Note, surgeryIndex
is dynamic so I can't hard-code anything.
The update code above produces this error:
Exception while simulating the effect of invoking '/patients/update'
errorClass {
error: 409,
reason: "MinimongoError: can't append to array using string field name [surgeryIndex]",
details: undefined, message: "MinimongoError: can't append to array using string field name [surgeryIndex] [409]",
errorType: "Meteor.Error"}
Best way is to take the array out, modify it and update it back with $set
query.
If you really want to do the way you want, you need to $pull the value from the array and then you have to push it to specific index using $position operator.
But $position
operator introduced in version 2.6. In case if you are using older than that, as far as i know you don't have any other choice than my first suggestion.