I’m trying to fashion an aggregation that’ll tell me which director in the movies
database has the highest average metecritic score. Here is what I have so far:
dirAgg=[
{
$group: {
_id: "$directors",
num_films_directed: { $sum: 1 },
avg_mtc: { $avg: "$metacritic" }
}
},
{
$sort: { "avg_mtc": -1 }
}
]
The problem is that directors
is an array field. I’ve been trying to fashion an aggregation that’ll group the documents by individual director - but the only solution I can think of is re-doing the entire movies
collection so each movie has duplicate documents for each director with only one director name in the field (e.g. “Fargo” would have two entries, one for Joel Coen and one for Ethan Coen).
There has to be a better way than that, but I’m having trouble wrapping my head around it.