概述
我有这条线:
def update(self,recid): self.collection.find_and_modify(query={"recid":recid},update={{ "$set": {"creation_date":str( datetime.Now() ) }}} )
抛出TypeError:Unhashable类型:’dict’.
此函数仅用于查找recid与参数匹配的文档并更新其creation_date字段.
为什么会出现这个错误?
self.collection.find_and_modify(query={"recid":recid},update={"$set": {"creation_date": str(datetime.Now())}})
UPD(解释,假设你在python> = 2.7):
发生错误是因为python认为您正在尝试使用{}表示法创建一个集合:
The set classes are implemented using dictionaries. Accordingly,the
requirements for set elements are the same as those for dictionary
keys; namely,that the element defines both __eq__() and __hash__().
换句话说,集合的元素应该是可以清除的:例如,int,string.而你正在传递一个字典,它不是可以清洗的,也不能成为一个集合的元素.
另外,请看这个例子:
>>> {{}} Traceback (most recent call last): File "<stdin>",line 1,in <module> TypeError: unhashable type: 'dict'
希望有所帮助.
总结
以上是编程之家为你收集整理的python – Pymongo API TypeError:不可用的字典全部内容,希望文章能够帮你解决python – Pymongo API TypeError:不可用的字典所遇到的程序开发问题。
如果您也喜欢它,动动您的小指点个赞吧