0

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?

sridharnetha
  • 2,104
  • 8
  • 35
  • 69

1 Answers1

0

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.

Cuong Le Ngoc
  • 11,595
  • 2
  • 17
  • 39