You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 01:24:03