Reputation: 446
I am working on a query that need to update a subdocument in meteor mongo. My code looks like this:
Cases.update(
{"_id":doc._id,"notes._id": doc.noteid},
{$set:{
'notes.$': {
'note': doc.note,
'updatedBy': currentUser,
'updatedAt': date
}
}});
this does work, however it does remove other fields that are not in the update such as "createdAt" and "date". I went as far as doing this but i get the same result:
Cases.update(
{"_id":doc._id,"notes._id": doc.noteid},
{$set:{
'notes.$': {
'note': doc.note,
'updatedBy': currentUser,
'updatedAt': date,
'createdBy':doc.createdBy,
'date': doc.date,
'_id':doc.noteid
}
}});
the notes.$._id, date and createdBy fields are removed in the opertion. I am using aldeed simple schema and collection 2 as packages of the autoForm package. This update is however been done using a generic form with a Meteor.call. I am using meteor 1.2.1. I know that collection 2 has the removeEmptyString setting turned on by default that is why I tried doing it the second way but it still does not work. Can some one please help?
Upvotes: 3
Views: 438
Reputation: 4049
Your $set is saying, "Assign the notes.$ object to this object I'm giving you, and delete whatever is currently there."
You want to do something like:
Cases.update( {"_id":doc._id, "notes._id": doc.noteid},
{
$set:{
'notes.$.note': doc.note,
'notes.$.updatedBy': currentUser,
'notes.$.updatedAt': date
}
});
My $set is saying, "Assign these three particular fields of the notes.$ object to the values I'm giving you, and don't touch anything else."
Upvotes: 4