0

I have a douments like as follows.

How do I update a skillcluster name. Suppose the other document has name :"c" in 4th position.

{
  Job: {
    post: { name:"x" } 
    skill: { 
      skillcluster: [
        {name:"c++",id:"23"},
        {name:"c",id:"898"}
      ]
    }
  }
}
{
  Job: {
    post: { name:"x" } 
    skill: {
      skillcluster: [
        {name:"c++",id:"23"},
        {name:"java"},
        {name:"python"},
        {name:"c",id:"898"}
      ]
    }
  }
}
Blakes Seven
  • 49,422
  • 14
  • 129
  • 135
sara
  • 115
  • 1
  • 2
  • 15
  • What are you asking here? Which document do you want to update and with what? Plese note the cleanup that has already been done on your question and try to be a clear as possible. – Blakes Seven Aug 25 '15 at 05:18
  • If skillcluster name "C" means i need to update that as "Simple C" at a time in the two documents or more. But that position is varied here. How I update that "C" as "Simple C". – sara Aug 25 '15 at 05:24
  • possible duplicate of [MongoDB - Update an object in nested Array](http://stackoverflow.com/questions/10522347/mongodb-update-an-object-in-nested-array) – Neo-coder Aug 25 '15 at 05:34

1 Answers1

0

You need to query to match the "name" field at the embedded level of the document using "dot notation", and then pass that match with the positional $ operator within the update:

db.collection.update(
  { "Job.skill.skillcluster.name": "c" },
  { "$set": { "Job.skill.skillcluster.$.name": "Simple C"}},
  { "multi": true }
)

Also use the "multi" flag to match and update more than one document.

The result will be:

{
    "_id" : ObjectId("55dbfd0ed96d655eb0ed2b4f"),
    "Job" : {
            "post" : {
                    "name" : "x"
            },
            "skill" : {
                    "skillcluster" : [
                            {
                                    "name" : "c++",
                                    "id" : "23"
                            },
                            {
                                    "name" : "Simple C",
                                    "id" : "898"
                            }
                    ]
            }
    }
}
{
    "_id" : ObjectId("55dbfd0ed96d655eb0ed2b50"),
    "Job" : {
            "post" : {
                    "name" : "x"
            },
            "skill" : {
                    "skillcluster" : [
                            {
                                    "name" : "c++",
                                    "id" : "23"
                            },
                            {
                                    "name" : "java"
                            },
                            {
                                    "name" : "python"
                            },
                            {
                                    "name" : "Simple C",
                                    "id" : "898"
                            }
                    ]
            }
    }
}
Blakes Seven
  • 49,422
  • 14
  • 129
  • 135
  • Ya its just update the 0 th position values. but it does not affects the other positions – sara Aug 25 '15 at 05:54
  • @sara I have no idea what you are talking about. Look at the results above. Each "varied" position where `"name": "c"` is now changed to `"name": "Simple C"` wherever it was matched. If you mean that you have "more than one" value for `"name": "c"` within each array then you need to read the documentation on the "positional operator" as provided in the link in the question. But this was also not the question that you asked. You samples have one occurance only. – Blakes Seven Aug 25 '15 at 06:02
  • Consider, The "C" in the first position in the first document and then I try the query. But it does not update the 2nd documents 3rd position value – sara Aug 25 '15 at 06:16
  • @sara You might need someone to translate. Take a look at all the details in the answer again and you will clearly see that the "name" being matched is at different array positions. My update performed on your data. If you get something different then you are doing something different. You certainly are not making yourself clear. – Blakes Seven Aug 25 '15 at 06:20
  • Actually i just mention here the two documents only. I have an another document with the name as "c" in the 0 th position. In that document the update is not happens. – sara Aug 25 '15 at 06:25
  • @sara See how your question contains two documents and certain conditions? See how the answer addresses the problem that you asked? Good. Now Welcome to stackoverflow. You [accept](http://stackoverflow.com/help/accepted-answer) the answer that answers what you asked. Then you [ask a new question](http://stackoverflow.com/questions/ask) about the new thing you want to ask and carefully include all of the details. You got your answer here. And the best place for you to explain new questions is with a new question. – Blakes Seven Aug 25 '15 at 06:30
  • Okay. Thank you so much . It's working. Thanks a lot – sara Aug 25 '15 at 06:32