简体   繁体   中英

Update command in mongoose is removing old sub document

I have the following document in a collection, here address is a sub document

{
 name:'Belmont Race', 
 address:{city:'Brentwood', state:'TN', postal:'35704'}
}

Here is my UPDATE query in mongoose,

Let where={name:'Belmont Race'};
Let set={$set:{'address.state':'PN'}};
companies.updateMany(where,set, (err,res)=>{
if(err){console.log(err);}
// do something
})

Expected output is,

{name:'Belmont Race', address:{city:'Brentwood', state:'PN', postal:'35704'}}

But it removing old entire address sub document and giving the output like below

{name:'Belmont Race', address:{ state:'TN' }}

Any Idea?

Refer to this , you just need to change this line Let set={$set:{'address.state':'PN'}}; to Let set={'address.state':'PN'}; then your problem will be solved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM