Reputation: 219
Here is some sample document(I only show used filed, there are other fields in document):
{"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}}
What I want do is to compute the score distribution, I use the bucket operation, according to 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))
However, I met the error:
"count": {"$sum": 1}
TypeError: unhashable type: 'dict'
I can't find the reason why there is type error, can anyone give some suggestion?
Upvotes: 0
Views: 660
Reputation: 219
Sorry guys, I made a super edit mistake. The pipeline should be array not a 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}
}
}}
]
Upvotes: 1