javascript - MongoDB: Set $unset with empty parameter gives 409 error -
i try set or unset values dynamically. have both, not always. do:
collection.update( { _id: id }, { $set: data, $unset: remove }, function (error) { console.warn(error); } );
this working, if data
, remove
set. there isn't have removed. if remove = {}
, 409 error, remove
empty.
how have improve update?
you need check size of remove
object before passing in:
var update = { $set: data } if (object.keys(remove).length > 0) { update['$unset'] = remove; } collection.update( { _id: id }, update, function (error) { console.warn(error); } );
Comments
Post a Comment