如何更新 MongoDB 中的特定数组元素

Sac*_*ach 3 mongodb spring-mongo

我是 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"
Run Code Online (Sandbox Code Playgroud)

我想将值更新为 11,其名称为Category. 我试过下面的查询:

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

但是 Mongo 回复了

can't append to array using string field name [value]
Run Code Online (Sandbox Code Playgroud)

如何形成一个将更新特定值的查询?

Joh*_*yHK 5

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

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