I'm following the aggregation pipeline course on MongoDB university.
Here is how the "movies" collection is formatted.
{
"_id" : ObjectId("573a1390f29313caabcd4cf1"),
"title" : "Ingeborg Holm",
"cast" : [
"Aron Lindgren",
"Erik Lindholm",
],
"imdb" : {
"rating" : 7,
},
}
Here is my aggregation:
db.movies.aggregate( [
{ $unwind: { path: "$cast" } },
{ $set: { "average": 0 } },
{ $group: {
_id: "$cast",
numFilms: { $sum: 1 },
average: { $avg: { $add: ["$average", "$imdb.rating"] } }, //failed here
}}
] )
What I'm trying to do:
Calculate the average of rating (imdb.rating) based for every movies of each cast.
What currently happen:
An error occured at
$add: ["$average", "$rating"]
with the follwoing message error:
"errmsg" : "$add only supports numeric or date types, not string",
Question:
Does anyone can explain why $rating is consider as a string ?
I provide above a example of how the "movies" collection is formatted. It seem that $add failed with "$imdb.rating" only when called within $group (and work normally outside of $group when using "$imdb.rating")
Is there another way to calculate the average ?
imdb.rating: ""? - Tushar Gupta - curioustushar""values or set them to0. Or you can use$convertas mentioned in the answer. - Tushar Gupta - curioustushar