当前位置: 首页 > 知识库问答 >
问题:

mongodb更新无法处理嵌套的子文档

司空和悌
2023-03-14

我的mongoDB记录就像在这个链接更新嵌套数组mongob和示例记录如下,想要更新嵌套文档“参数”数组中的一个字段,前提是它满足一些条件(_id:“04”,操作。_id:“100”和operations.parameters.pid:“012”),此更新查询UPDATES错误嵌套记录(operations.parameters.pid:“011”),请帮助我哪里出错了:

       {
"_id" : "04",
"name" : "test service 4",
"id" : "04",
"version" : "0.0.1",
"title" : "testing",
"description" : "test",
"protocol" : "test",
"operations" : [ 
    {
        "_id" : "99",
        "oName" : "test op 52222222222",
        "sid" : "04",
        "name" : "test op 52222222222",
        "oid" : "99",
        "description" : "testing",
        "returntype" : "test",
        "parameters" : [ 
            {
                "oName" : "Param1",
                "name" : "Param1",
                "pid" : "011",
                "type" : "582",
                "description" : "testing",
                "value" : "",
                "version" : 1.0
            }, 
            {
                "oName" : "Param2",
                "name" : "Param2",
                "pid" : "012",
                "type" : "58222",
                "description" : "testing",
                "value" : "",
                "version" : 2.0
            }
        ]
    }, 
    {
        "_id" : "100",
        "oName" : "test op 909090",
        "sid" : "05",
        "name" : "test op 90909",
        "oid" : "1009",
        "description" : "testing",
        "returntype" : "test",
        "parameters" : [ 
            {
                "oName" : "Param1",
                "name" : "Param1",
                "pid" : "011",
                "type" : "582",
                "description" : "testing",
                "value" : "",
                "version" : 1.0
            }, 
            {
                "oName" : "Param2",
                "name" : "Param2",
                "pid" : "012",
                "type" : "58222",
                "description" : "testing",
                "value" : "",
                "version" : 2.0
            }
        ]
    }, 
    {
        "_id" : "101",
        "oName" : "test op 52222222222",
        "sid" : "04",
        "name" : "test op 52222222222",
        "oid" : "99",
        "description" : "testing",
        "returntype" : "test",
        "parameters" : [ 
            {
                "oName" : "Param1",
                "name" : "Param1",
                "pid" : "011",
                "type" : "582",
                "description" : "testing",
                "value" : "",
                "version" : 1.0
            }, 
            {
                "oName" : "Param2",
                "name" : "Param2",
                "pid" : "012",
                "type" : "58222",
                "description" : "testing",
                "value" : "",
                "version" : 1.0
            }
        ]
    }, 
    {
        "_id" : "102",
        "oName" : "test op 909090",
        "sid" : "05",
        "name" : "test op 90909",
        "oid" : "1009",
        "description" : "testing",
        "returntype" : "test",
        "parameters" : [ 
            {
                "oName" : "Param1",
                "name" : "Param1",
                "pid" : "011",
                "type" : "582",
                "description" : "testing",
                "value" : "",
                "version" : 1.0
            }, 
            {
                "oName" : "Param2",
                "name" : "Param2",
                "pid" : "012",
                "type" : "58222",
                "description" : "testing",
                "value" : "",
                "version" : 2.0
            }
        ]
    }
]
   }

我的更新查询如下:

 db.foo.update(
{ $and : [{'_id':'04'},
{'operations._id':'100' },
{'operations.parameters.pid': '012'}]},
{
    "$set": { 
        "operations.1.parameters.$.dummy": "foo"
    }
}
     )

我使用mongoDB 3.6.2参考https://docs.mongodb.com/master/reference/operator/update/positional-filtered/示例记录从这个链接:

   {
"_id" : 1.0,
"grades" : [ 
    {
        "type" : "quiz",
        "questions" : [ 
            10.0, 
            8.0, 
            5.0
        ]
    }, 
    {
        "type" : "quiz",
        "questions" : [ 
            8.0, 
            9.0, 
            6.0
        ]
    }, 
    {
        "type" : "hw",
        "questions" : [ 
            5.0, 
            4.0, 
            3.0
        ]
    }, 
    {
        "type" : "exam",
        "questions" : [ 
            25.0, 
            10.0, 
            23.0, 
            0.0
        ]
    }
]
   }

此链接中的示例

   db.student3.update(
   {},
        { $inc: { "grades.$[t].questions.$[score]": 2 } },
     { arrayFilters: [ { "t.type": "quiz" } , { "score": { $gte: 8 } } ], multi: true}
      )

我从robo-3t得到的错误:

 cannot use the part (grades of grades.$[t].questions.$[score]) to traverse the element ({grades: [ { type: "quiz", questions: [ 10.0, 8.0, 5.0 ] }, { type: "quiz", questions: [ 8.0, 9.0, 6.0 ] }, { type: "hw", questions: [ 5.0, 4.0, 3.0 ] }, { type: "exam", questions: [ 25.0, 10.0, 23.0, 0.0 ] } ]})

请帮忙;问候克里斯

共有1个答案

元修然
2023-03-14

在更新操作中

"$set": { 
        "operations.1.parameters.$.dummy": "foo"
    }

引用操作中的第一个元素,它是具有"_id":"100",和参数数组中的元素,$更新数组中的第一个元素。

如果要使用$[]更新嵌套数组元素以更新所有匹配元素,则需要考虑使用mongodb 3.6。

在3.4版本中,一种可能的方法是获取所需的子文档,并在应用程序端进行匹配和更新。

 类似资料:
  • 我有一个深度嵌套的文档结构,如下所示: 我正在尝试更新集合以插入新配置,如下所示: 我正在mongo(Python)中尝试类似的内容: 但是,我得到了“如果没有包含数组的相应查询字段,则无法应用位置运算符”错误。在mongo这样做的正确方式是什么?这是mongo v2。4.1.

  • 我想在具有指定 URL 的相应文档中将嵌套的“已爬行”更新为 True。 我对mongodb相当陌生,我似乎无法弄清楚这一点,非常感谢任何帮助。

  • 我正在尝试更新嵌套数组中的值,但无法使其工作。 我的对象是这样的 提前谢谢!

  • 本文向大家介绍MongoDB查询中如何更新嵌套文档,包括了MongoDB查询中如何更新嵌套文档的使用技巧和注意事项,需要的朋友参考一下 要更新嵌套文档,请使用update(),并在其中使用点号。让我们创建一个包含文档的集合- 在find()方法的帮助下显示集合中的所有文档- 这将产生以下输出- 以下是更新嵌套文档的查询- 在find()方法的帮助下显示集合中的所有文档- 这将产生以下输出-

  • 我有一个模型看起来是这样的: 我正在使用存储库模式,下面是我的代码片段: 这实际上会引发以下错误: 我目前的做法 当前对我有效的是在更新嵌套对象时使用。然而,这提出了几点考虑: 需要额外的代码来获取整个文档、解析和更新必需的字段 由于在文档级别上工作,因此它还将更新文档中未更改的部分,这并不理想。

  • 将mongodb与pymongo一起使用,我有以下文档: 我想更新示例子文档(这是一个数组元素,因为可能有多个示例)。我有以下代码,但它不工作... 谁能告诉我这个有什么问题吗?