Pymongo API TypeError: Unhashable dict

后端 未结 1 1643
臣服心动
臣服心动 2021-01-04 11:22

I\'m writing an API for my software so that it is easier to access mongodb.

I have this line:

def update(self, recid):        
    self.collection.fi         


        
相关标签:
1条回答
  • 2021-01-04 11:52

    It's simple, you have added extra/redundant curly braces, try this:

    self.collection.find_and_modify(query={"recid":recid}, 
                                    update={"$set": {"creation_date": str(datetime.now())}})
    

    UPD (explanation, assuming you are on python>=2.7):

    The error occurs because python thinks you are trying to make a set with {} notation:

    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__().

    In other words, elements of a set should be hashable: e.g. int, string. And you are passing a dict to it, which is not hashable and cannot be an element of a set.

    Also, see this example:

    >>> {{}}
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unhashable type: 'dict'
    

    Hope that helps.

    0 讨论(0)
提交回复
热议问题