How to update particular array element in MongoDB

前端 未结 2 1164
孤街浪徒
孤街浪徒 2020-12-21 16:31

I am newbie in MongoDB. I have stored data inside mongoDB in below format

\"_id\" : ObjectId(\"51d5725c7be2c20819ac8a22\"),
\"chrom\" : \"chr22\",
\"pos\" :          


        
相关标签:
2条回答
  • 2020-12-21 17:04

    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'}
    })
    
    0 讨论(0)
  • 2020-12-21 17:10

    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'}
     }
    )
    
    0 讨论(0)
提交回复
热议问题