Chapter 4: Core Aggregation - Multidimensional Grouping Lab - $facets -- no intersecting elements. please see description not same as existing topics

I ran two pipelines prior to trying $facets and I found no matching elements.

db.movies.aggregate([{$match: {"imdb.rating":{$exists:true},"imdb.rating":{$ne:null},"imdb.rating":{$ne:""}}},{$sort:{"imdb.rating": -1}},{$limit:10}, {$project:{title: 1 ,"imdb.rating": 1,"_id":0}}]).pretty()

that results in

{ "title" : "Band of Brothers", "imdb" : { "rating" : 9.6 } }
{
        "title" : "The Third Annual 'On Cinema' Oscar Special",
        "imdb" : {
                "rating" : 9.5
        }
}
{ "title" : "Planet Earth", "imdb" : { "rating" : 9.5 } }
{ "title" : "The Chaos Class", "imdb" : { "rating" : 9.5 } }
{ "title" : "Dances Sacred and Profane", "imdb" : { "rating" : 9.5 } }
{ "title" : "Thani Oruvan", "imdb" : { "rating" : 9.4 } }
{
        "title" : "A Brave Heart: The Lizzie Velasquez Story",
        "imdb" : {
                "rating" : 9.4
        }
}
{ "title" : "Drag Becomes Him", "imdb" : { "rating" : 9.4 } }
{ "title" : "The Civil War", "imdb" : { "rating" : 9.4 } }
{ "title" : "The Real Miyagi", "imdb" : { "rating" : 9.3 } }

Also ran the other set

db.movies.aggregate([{$match: {"metacritic":{$ne:null}}},{$sort:{"metacritic": -1}},{$limit:10}, {$project:{title: 1 ,"metacritic": 1,"_id":0}}]).pretty()
{ "metacritic" : 100, "title" : "The Wizard of Oz" }
{ "metacritic" : 100, "title" : "The Leopard" }
{ "metacritic" : 100, "title" : "The Godfather" }
{ "metacritic" : 100, "title" : "The Conformist" }
{ "metacritic" : 100, "title" : "Sweet Smell of Success" }
{ "metacritic" : 100, "title" : "Lawrence of Arabia" }
{ "metacritic" : 100, "title" : "Journey to Italy" }
{ "metacritic" : 100, "title" : "Fanny and Alexander" }
{ "metacritic" : 100, "title" : "Boyhood" }
{ "metacritic" : 100, "title" : "Best Kept Secret" }

What am I missing here. There is no indication in the question that both imdb.rating and metacritic should be present on the title.

Any help is appreciated.

Hi @Vijay_Tripuraneni,
The question states that:

How many movies are in both the top ten highest rated movies according to the imdb.rating and the metacritic fields? We should get these results with exactly one access to the database.

Since many movies can have the same metacritic/imdb rating, it would be difficult to make sure that everybody gets a consistent answer, so, within each facet after you are done with sorting the documents in the descending order on the basis of the metacritic/imdb score, please sort them on the basis of their titles in ascending order, like this:

{ "$sort": { "metacritic": -1, title: 1 } }
{ "$sort": { "imdb.rating": -1, title: 1 } }

And hopefully, you will see some documents intersecting in both the results.

In case you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,
MongoDB

1 Like