更新数组中的匹配对象

时间:2018-05-16 09:00:07

标签: mongodb pymongo

我正在尝试更新MongoDB文档中的嵌套值,但是我收到以下错误: "不能使用部分(Accounts of Accounts.Rules.Frequency)来遍历元素"

以下是我的文档结构和我尝试运行的代码:

{"_id" : ObjectId("5ab2313bf7951607c1072e54"),
 "Customer_id" : 2,
 "Accounts" : 
  [ {"Account_id" : NumberLong(1398868310133024),
    "AccountType" : "admin",
    "Created_at" : "01-01-2018",
    "Rules" : {"Goal" : 200,
               "DoNotDisturbFrom" : "22",
               "DoNotDisturbTo" : "8",
               "Frequency" : "weekly"}
    }, 
   {"Account_id" : 2,
    "AccountType" : "user",
    "Created_at" : "01-02-2018",
    "Rules" : {"Goal" : 200,
               "DoNotDisturbFrom" : "20",
               "DoNotDisturbTo" : "12",
               "Frequency" : "monthly"}
    }
 ]
}

以下是代码:

result = collection.update_one({"Accounts.Account_id":1398868310133024}, {'$set':{"Accounts.Rules.Frequency":"monthly"}})
print result

0 个答案:

没有答案