您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python – Pymongo API TypeError:不可用的字典

5b51 2022/1/14 8:22:18 python 字数 2152 阅读 522 来源 www.jb51.cc/python

我正在为我的软件编写API,以便更容易访问 mongodb. 我有这条线: def update(self, recid): self.collection.find_and_modify(query={"recid":recid}, update={{ "$set": {"creation_date":str( datetime.now() ) }}} ) 抛出TypeE

概述

我有这条线:

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:不可用的字典所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶