具有不可哈希类型的 MongoDB 存储桶操作:mongoEngine 中的"dict"



这是一些示例文档(我只显示使用过的归档,文档中还有其他字段(:

{"id":1111,"info":{"score":90,"class":"algorithm","room":101}}
{"id":1112,"info":{"score":60,"class":"java","room":401}}
{"id":1113,"info":{"score":100,"class":"python","room":301}}

我要做的是计算分数分布,我使用桶运算,根据 https://docs.mongodb.com/manual/reference/operator/aggregation/bucket/index.html。

pipeline = {
{"$match": {"_id": ObjectId(_id)}},
{"$bucket": {
"groupBy": "$info.score",
"boundaries": [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100],
"output": {
"count": {"$sum": 1}
}
}}
}
score_distribution = list(Students.objects.aggregate(*pipeline))

但是,我遇到了错误:

"count": {"$sum": 1}
TypeError: unhashable type: 'dict'

我找不到类型错误的原因,任何人都可以给出一些建议吗?

对不起,伙计们,我犯了一个超级编辑错误。 管道应该是数组而不是字典。

pipeline = [
{"$match": {"_id": ObjectId(_id)}},
{"$bucket": {
"groupBy": "$info.score",
"boundaries": [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100],
"output": {
"count": {"$sum": 1}
}
}}
]

最新更新