javascript - Remove object fields by updating the complete object -


this data stored in mongodb document:

{      _id: "123"     order: 1     parent: "dueqmd64ntxm3u9cm"     type: "article"     unit: "kg" } 

while saved data calculated , validated first (that's why don't use data = { order: 2 }), i'm using complete object update document:

var data = {      order: 2     parent: "dueqmd64ntxm3u9cm"     type: "article"     unit: "kg" }  collection.update(     { _id: 123 },      { $set: data } ); 

this working.

but if want remove values, doesn't work:

var data = {      order: 2     parent: "dueqmd64ntxm3u9cm"     type: "article"     unit: undefined }  collection.update(     { _id: 123 },      { $set: data } ); 

i'm expecting unit-field removed. isn't case...

to remove fields, use $unset operator:

data = {     unit: "" }; collection.update(     { _id: 123 },      { $set: data } ); 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -