Update a nested array objects in a different collection and position in MongoDB -
i have douments follows.
how update skillcluster name. suppose 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"} ] } } }
you need query match "name" field @ embedded level of document using "dot notation", , pass match positional $
operator within update:
db.collection.update( { "job.skill.skillcluster.name": "c" }, { "$set": { "job.skill.skillcluster.$.name": "simple c"}}, { "multi": true } )
also use "multi" flag match , update more 1 document.
the result 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" } ] } } }
Comments
Post a Comment