How to update particular array element in MongoDB

牧云@^-^@ 提交于 2019-11-29 16:49:56

You can use the $ positional operator to identify the first array element to match the query in the update like this:

db.VariantEntries.update({
    "pos": 17060409,
    "sampleID": "Job1373964150558382243283", 
    "information.name":"Category"
},{
    $set:{'information.$.value':'11'}
})

In MongoDB you can't adress array values this way. So you should change your schema design to:

"information" : {
    'category' : 3,
    'INDEL' : INDEL
    ...
}

Then you can adress the single fields in your query:

db.VariantEntries.update(
 {
    {"pos" : 117199533} , 
    {"sampleID" : "Job1373964150558382243283"},       
    {"information.category":3}
 },
 {
   $set:{'information.category':'11'}
 }
)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!