After spending some time on this:
Chapter 4: Core Aggregation - Multidimensional Grouping
Lab - $facets
I did quite a bit of trial and errors to come to the conclusion that the following pipeline should work, giving us the base information needed along with the final solution. And looking at how the problem is formulated, I still think it is the way to go. It also complies to the reformulation found here.
db.movies.aggregate([
{$facet: {
imdb:[
{$match:{"imdb.rating":{$gte:9.3}}},
{$sort:{"imdb.rating":-1}},
{$project:{_id:0,title:1,"imdb.rating":1}}
],
metacritic:[
{$match:{metacritic:{$gte:100}}},
{$sort:{metacritic:-1}},
{$project:{_id:0,title:1,metacritic:1}}
],
imdb_metacritic:[
{$match:{
"imdb.rating":{$gte:9.3},
metacritic:{$gte:100}
}},
{$project:{_id:0,title:1}}
]
}
}
]).pretty()
I am aware the number of elements in imdb
and metacritic
is going slightly over 10, but this is OK because looking at the values for imdb.rating
and metacritic
there is no way to decide on which element to eliminate. And more importantly the result number of elements in imdb_metacritic
is 0.
But this answer is not in the list of choices.
After spending more time, reading some of the many discussion posts on the subject, I see that a number of people agree that this problem is nothing but clear. I have seen some hints, supposedly leading to “the solution”, but since they did not made much sense to me, I didn’t follow on that path.
For example, adding this to the pipeline I described above:
{$match: { "imdb.rating": { $exists: true } }}, {$match: { "metacritic": { $exists: true } }},
Regardless of knowing if it leads to “the solution” or not.
I don’t see on which ground I would be allowed to require:
{$match: { "imdb.rating": { $exists: true } }}
in order to later find the top 10 in the metacritic
based classification.
And by the same token, why would one be allowed to require:
{$match: { "metacritic": { $exists: true } }}
in order to later find the top 10 in the imdb.rating
based classification.
If someone can make sense of this lab, I hope to get some feedback.