2013-07-16 90 views
0

我是MongoDB的新手。我已經存儲在以下格式如何更新MongoDB中的特定數組元素

"_id" : ObjectId("51d5725c7be2c20819ac8a22"), 
"chrom" : "chr22", 
"pos" : 17060409, 
"information" : [ 

     { 
       "name" : "Category", 
       "value" : "3" 
     }, 
     { 
       "name" : "INDEL", 
       "value" : "INDEL" 
     }, 
     { 
       "name" : "DP", 
       "value" : "31" 
     }, 
     { 
       "name" : "FORMAT", 
       "value" : "GT:PL:GQ" 
     }, 

     { 
       "name" : "PV4", 
       "value" : "1,0.21,0.00096,1" 
     } 
], 
"sampleID" : "Job1373964150558382243283" 

我想更新價值11其名稱爲Category內MongoDB的數據。 我曾嘗試下面的查詢:

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

但蒙戈回覆

can't append to array using string field name [value] 

怎樣才能形成一個查詢將更新特定的價值?

+0

你有沒有看着http://stackoverflow.com/questions/13040344/cant-append-to-array-using-string-field-name-when-performing-update -on-ARRA –

回答

2

可以使用$位置操作,以確定第一個數組元素匹配查詢在這樣的更新:

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

在MongoDB中,您無法以這種方式來處理數組值。所以,你應該改變你的架構設計:

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

然後你就可以ADRESS查詢中的單場:

db.VariantEntries.update(
{ 
    {"pos" : 117199533} , 
    {"sampleID" : "Job1373964150558382243283"},  
    {"information.category":3} 
}, 
{ 
    $set:{'information.category':'11'} 
} 
) 
相關問題