使用pymongo时出现TypeError: unhashable type: 'list'如何解决
报错原因
- pymongo的
collection.find()方法第一个参数要求为单个字典类型的查询过滤条件,所有匹配规则都需要以键值对的形式写在同一个字典内。你当前将三个独立的条件字典放在外层大括号(等价于创建包含多个字典的集合)或方括号(等价于列表)中,而字典/列表均属于不可哈希类型,不符合参数要求,因此触发TypeError: unhashable type: 'list'报错。
修复方案
将多个查询条件合并到同一个字典内作为find()的第一个参数即可,修正后的代码如下:
cursor = collection.find( { "colourCode" : "orange", "createdOn": { "$gt" : my_datetime }, "assignmentRef": {'$ne': None} }, { "createdOn": 1, "assignmentRef.name": 1, "_id": 0, "colourCode": 1 } ) list_cur = list(cursor) df = DataFrame(list_cur) print(df)
内容的提问来源于stack exchange,提问作者usmansharif shaik
相关产品推荐
相关产品推荐

