1

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?

3
  • 1
    Could you try db.patient.updateOne({firstName:"Thanga","history.disease":"Cold"}, {$set:{lastName:"Yuvi","age":28,"history.disease.$":"Fever"}}) ? Commented May 6, 2019 at 10:38
  • @mickl thank you so much!! it is working perfectly Commented May 9, 2019 at 5:45
  • Will post it as an answer, maybe it will help someone Commented May 9, 2019 at 6:10

1 Answer 1

1

try this

db.patient.update(
  {
    "firstName":"Thanga",
    "history.disease":"Cold"
  }, 
  {
    $set:{
      "lastName":"Yuvi",
      "age":28,
      "history.disease.0":"Fever"
   }
  })
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.