'PyMongo aggregation function
Need solution of code which is aimed at finding out the total number of movies by genre. The code I am using is:
agg_result = movies_collection.aggregate(
[{"$match": {}},{"$group":
{"_id":"$genres",
"Total movie count":{"$sum":1}
}}
])
for i in agg_result:
print(i)
I need movies by each genre but since uploaded csv file has multiple entries in genre field the sample output is coming wrong.
Sample output coming as: {'_id': 'Animation|Children|Comedy|Fantasy', 'Total movie count': 6} {'_id': 'Action|Drama|Horror|IMAX', 'Total movie count': 1}
Solution 1:[1]
Query
- split the genre string on "|"
- unwind so each document 1 genre
- group and count the genres
*i think you should also update the collection and make the genre a normal array, having strings with "|" doesn't seem good schema.
aggregate(
[{"$set": {"genres": {"$split": ["$genres", "|"]}}},
{"$unwind": "$genres"},
{"$group": {"_id": "$genres", "count": {"$sum": 1}}}])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Takis |
