Vivek Harry
Vivek Harry

Reputation: 439

Update nth array value under embedded documents in MongoDB

I want to update only the nth position in that array, Instead of updating all the values in the array. db.patient.insertMany([ {firstName: "Thanga",lastName:"Durai", age:27,history:{disease:["Cold","ulergy"]}}]);

Here, Can you please tell me how to update value "Cold" into "Fever", remains are the same.

I already know another method to archive this, but using this I have to update all the values in an array: db.patient.updateOne({firstName:"Thanga","history.disease":"Cold"}, {$set:{lastName:"Yuvi","age":28,"history.disease":["Fever","Ulergy"]}} )

Can someone please tell me how to update only Cold into Fever?

Upvotes: 1

Views: 47

Answers (1)

Dandi Kantesh
Dandi Kantesh

Reputation: 29

try this

db.patient.update(
  {
    "firstName":"Thanga",
    "history.disease":"Cold"
  }, 
  {
    $set:{
      "lastName":"Yuvi",
      "age":28,
      "history.disease.0":"Fever"
   }
  })

Upvotes: 1

Related Questions