Shanthi B
Shanthi B

Reputation: 143

How to updates nested object using mongoose?

i'm new to mongoose, I want to update my city_name, my schema structure looks like

   const addressSchema = new mongoose.schema({
     address:{
       door_no:{type:number},
       other_details:{
         street_name:{ type: string},
         city:{
           city_name:{type:string},
           pincode:{type:string},
          }
       }
     }
    })

sample db data :

{
  "_id": "63dw8sdhs8ad0s",
  "address": {
    "door_no": 43,
    "other_details": {
      "street": "sdsadada",
      "city": {
        "city_name": "dfaef"
      }
    }
  }
}

now i want to update the city_name i tried this query

addressModel.findByIdAndUpdate({_id:id},{$set:{'address.other_details.city.city_name':'44xd3xc'}})

but it doesn't update for me.

Upvotes: 0

Views: 143

Answers (1)

nodejs developer
nodejs developer

Reputation: 109

check that you passing id crctly or not

https://mongoplayground.net/p/ZDPiTrMUHha

Upvotes: 1

Related Questions