node.js - Why can't I seem to merge a normal Object into a Mongo Document? -
i have data feed 3rd party server pulling in , converting json. data feed never have mongodb's auto-generated _id
s in it, there unique identifier called vehicle_id
.
the function below handling taking data-feed generated json object fresh_event_obj
, copying values mongo document if there mongo document same vehicle_id
.
function update_vehicle(fresh_event_obj) { console.log("updating vehicle " + fresh_event_obj.vehicleid + "..."); vehicle.find({ vehicleid: fresh_event_obj.vehicleid }, function (err, event_obj) { if (err) { handle_error(err); } else { var updated = _.merge(event_obj[0], fresh_event_obj); updated.save(function (err) { if (err) { handle_error(err) } else { console.log("vehicle updated"); } }); } }); }
the structures of event_obj[0]
, fresh_event_obj
identical, except event_obj[0]
has _id
, __v
while "normal" object doesn't.
when run _.merge on these two, or own recursive function copies values latter former, nothing in updated
object different event_obj[0]
, despite fresh_event_obj
having new values.
does have idea i'm doing wrong? feel obvious , i'm failing see it.
the problem if don't have properties defined in schema, , if don't exist, can't create them
doc.prop = value
even if have {strict:false}
in schema.
the way set new properties
doc.set('prop', value)
(you still have have {strict:false}
in schema if property doesn't exist in schema)
as having many properties defined in schema, can use for-in loop go through object properties
for(key in fresh_event_obj) event_obj.set(key, fresh_event_obj[key]);
Comments
Post a Comment