I have a JSON document stored in MongoDB that has the following structure:
{
"_id" : "cfqjJW8WZprDSJEop",
"rName" : "z1",
"pName" : "P-4",
"ipAddress" : "21.1.1.12",
"misc" : {
"createdBy" : "admin",
"updatedBy" : "admin",
"creationTime" : ISODate("2016-09-15T09:43:10.953Z"),
"updatedTime" : ISODate("2016-09-15T09:43:10.953Z")
}
}
In my Meteor application, I have written code in the helper function to update only the updatedBy
and updatedTime
fields of this document during each update operation.
The misc
object is dynamically added just before inserting or updating the record.
However, when I try to update the record using my code snippet below, the entire misc
object gets replaced with the new values, causing the loss of createdBy
and creationTime
fields:
doc = // contains the updated document.
misc = {};
misc.updatedBy = //some name
misc.updatedTime = new Date();
doc.misc = misc,
r.update(id, doc); // calling Meteor's update method
I've also attempted to partially update the object by injecting something like this into the doc
:
doc.$set.misc.updatedBy
But it resulted in an error stating that updatedBy
does not exist. Can anyone suggest the correct approach to partially update an object within a document?