3

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

"_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"

I want to update the value to 11 which has the name as Category. I have tried below query:

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

but Mongo replies

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

How one can form a query which will update the particular value?

4

2 回答 2

6

您可以使用$位置运算符来识别第一个数组元素以匹配更新中的查询,如下所示:

db.VariantEntries.update({
    "pos": 17060409,
    "sampleID": "Job1373964150558382243283", 
    "information.name":"Category"
},{
    $set:{'information.$.value':'11'}
})
于 2013-07-16T17:05:57.453 回答
-2

在 MongoDB 中,您不能以这种方式处理数组值。因此,您应该将架构设计更改为:

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

然后您可以处理查询中的单个字段:

db.VariantEntries.update(
 {
    {"pos" : 117199533} , 
    {"sampleID" : "Job1373964150558382243283"},       
    {"information.category":3}
 },
 {
   $set:{'information.category':'11'}
 }
)
于 2013-07-16T14:02:56.530 回答