简体   繁体   中英

update sub-document in meteor mongo

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?

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."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM