node.js - Set field to empty for mongo object using mongoose -


i have document object has embedded sub-document. "clear" sub-document, try this:

obj.mysub = {}; obj.save(); 

this doesn't work, object still has contents of mysub sub-document. this:

obj.mysub = undefined; obj.save(); 

this work, removes sub-document object.

my question why doesn't first version work? going on in mongodb / mongoose in first example?

[edit] why doesn't empty object saved in first example above.

mongoose sort of "protects" lot of logic have presented in it's own internal resolution. if need @ lower level driver in:

yourmodel.update(    { /*statement matching document query */ },    { "$unset": { "mysub": 1 } } ) 

and per normal mongodb logic work , remove level in document selected. see $unset operator more.


Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -