【发布时间】:2017-04-10 10:19:40
【问题描述】:
我正在使用 Pymongo 来操作一个集合,女巫的结构如下所示:
{
Client1:{
"_id" : "Client1"
Project1:{
Software1:{
language : "Python",
complexity : "Low"
}
Software2:{
language : "C#",
complexity : "Low"
}
Software3:{
language : "Java",
complexity : "Hard"
}
}
}
}
在这种情况下,我尝试了多种方法来更新文档Client1,插入一个新的Software,但我找不到执行此操作的方法或函数。
所以我的问题是:在这个范围内,我如何插入一个新的Software 而不排除现有项目?
这是我尝试但没有奏效的方法:
client = "Client1"
client = collection.find_one({"_id": client})
project = "Project1"
software = str(raw_input("Enter with the software to be added to project: "))
language = str(raw_input("Enter with the software language: "))
complexity = str(raw_input("Enter with the software complexity: "))
collection.update({"_id" : client["_id"], }, {"$set" : {
client[project] : {software : {"language": language, "complexity" : complexity}}
}
})
我得到的错误是:unhashable type: 'dict' 在包含client[project] : {software : {"language": language, "complexity" : complexity}} 的行中
从现在开始,感谢您的关注!
【问题讨论】: