更新(或推/拉)mongo pymongo中的嵌套数组

时间:2018-03-09 14:39:26

标签: mongodb mongodb-query pymongo

这就是我的表格:


    db.tenant_users.find({"blocked_apps_v2.apps" : "Club"},{blocked_apps_v2:1}).pretty()
    {
        "_id" : ObjectId("5aa0e70d440c87004e024777"),
        "blocked_apps_v2" : [
            {
                "policy_name" : "p2",
                "apps" : [
                    "Box",
                    "Drop",
                    "Pase"
                ]
            },
            {
                "policy_name" : "p4",
                "apps" : [
                    "Box",
                    "Club",
                    "Drop",
                    "Copy"
                ]
            }
        ]
    }
    {
        "_id" : ObjectId("5aa0e712440c87016545b7ee"),
        "blocked_apps_v2" : [
            {
                "policy_name" : "p1",
                "apps" : [
                    "Box",
                    "Dropbox"
                ]
            },
            {
                "policy_name" : "p3",
                "apps" : [
                    "Box",
                    "Club"
                ]
            }
        ]
    }

我想写一个查询,我可以更改任何一个应用程序的名称。例如,对于所有用户,在所有策略中将“Box”更改为“Boxed”。

我试过了:

> db.tenant_user.update({"blocked_apps_v2.apps": "Box"}, { "$push" : {"blocked_apps_v2.$" : "Jive"} })

但没有任何反应:

WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })

尝试了这个:

> db.tenant_users.update({},{ $set: {"blocked_apps_v2.$[].apps.$[a]" : "Pot"} }, { arrayFilters: [  { "a": "Drop" } ], multi: true })

得到了这个:


    WriteResult({
        "nMatched" : 0,
        "nUpserted" : 0,
        "nModified" : 0,
        "writeError" : {
            "code" : 16837,
            "errmsg" : "cannot use the part (blocked_apps_v2 of blocked_apps_v2.$[].apps.$[a]) to traverse the element ({blocked_apps_v2: [ { policy_name: \"p2\", apps: [ \"Box\", \"Drop\", \"Pase\" ] }, { policy_name: \"p4\", apps: [ \"Box\", \"Club\", \"Drop\", \"Copy\" ] } ]})"
        }
    })

0 个答案:

没有答案