Lab - $facets How to get the list of top 10 for two different attributes using facets.?

Can anyone tell me a how to get the top n results in facets stage …?
Because from the tutorials, I can get the count between range of values, I can apply acccumulator inside output of bucket but I don’t understand the top n results from the use of facets, Later I have to use $setIntersactionss to get only result set matching in both the facets.

Sort and limit allows you to get top or bottom.

What is wrong with the below query, because it does not return the desired output -

db.movies.aggregate([
{
$facet:{
“ratings”: [
{ $match: { “imdb.rating”: { $ne: null } } },
{
$sort: { “imdb.rating”: -1 }
},
{
$limit: 10
}

],
"metacritic": [
{ 	$match: { "metacritic": { $ne: null } } },
{
$sort: { "metacritic": -1 }
},
{
$limit: 10
}	
]
}
},
{
$project: {
common: { $setIntersection: [ "$rating", "$metacritic"] }
}
}

])

I had a similar issue, the result was 0!

Then I discovered from other feedback that the exercise is not specific enough. What is not explained is that rather than compare the top 10 imdb films with the top 10 metacritic films, you must compare only films that have both a imdb.rating and metacritic value.

If you just match the top 10 imdb.rating films you will see none have a metacritic field/value.

3 Likes

Awesome - I was getting a 0. I am going to incorporate your information into my solution and see if I can get a valid result.

Hi @Tannis_Bo,

I can check that you have successfully completed the lab. :clap:

Pleas feel free to reach out if you have any additional questions.

Kind Regards,
Sonali