Lab $facets ---- Chapter 4

Hi Team,

Issue - I feel the solution which is given by you in the ‘See Detailed Answer’ is not considering all test cases and it will be wrong in few particular scenarios.
Reason - Since we can have N no. of movies having a rating. Now according to your solution if we just sort the records by ratings and take the top 10, then in that case you will miss out all the movies having the same rating as the 10th movie you selected. For eg: rating 9 have 5 movies and rating 8.5 have 10 movies, Then according to your solution the last 5 movies after sorting will be excluded having rating 8.5 since just 5 out of 10 of them will be selected.

Possible solution - We should group the movies by rating and also have an array with all movies pushed into an array field movieIds and also have the sum of count for each rating. The we should add the sort stage by rating. We will have records like below:

{
	"_id" : 9.6,
	"count" : 1,
	"movieIds" : [
			ObjectId("573a139ff29313caabd003c4")
	]
}

Then with $setWindowFields we can add two fields to track the total cumulative sum of count filed till the current record. filed1 with [ubounded, -1] and field2 with [unbounded, current].
We don’t mention the partition field so that whole data set becomes a single partition.
After that we can filter the data by filtering with field1 less than 10, and field2 will be greater than or equal to 10.

Thus we will have all the movieIds with us in the last rating record with us. Now, we can get all the movieIds in a single record by unwinding the records with movieids and then again grouping whole record in a single group and pushing all movieIds in a field.

So, the above process we can do inside facet for each type of rating and then finally we can intersect the two after facet.

Thanks,
Saurabh Burman

Point missed in the original explanation:

We can filter the data by filtering with (field1 lt 10 and field2 lt 10) OR (field1 lt 10 and field2 gte10)

Since we take till the record which just crosses or reached the cumulative count of 10.

Thanks,
Saurabh Burman

Hi @Saurabh_Burman, welcome to the community.

That’s a fair point, but as mentioned in the detailed answer, if you combine your metacritic / imdb.rating sort with the title of the movie, then the results will be consistent always.

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

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer

Hi @SourabhBagrecha ,
Thanks for your reply. You are right.
But my point was that it is not taking the exceptional test case into consideration where some movies have a tie with the movie which is on the 10th position. Then many records will be ignored if we chose just 10 movies simply because they appear on the top 10 result from the database.

May be the question been asked by the team in this lab is excluding that hypothetical test case. Considering that its completely ok.

Thanks,
Saurabh

Hi @Saurabh_Burman :wave: .
I will forward this feedback to the appropriate channel and please be rest assured that our team will be making a fix as soon as possible.
Having said that, we are working on a complete revamp of this course from scratch and will soon be opening it up for beta testing, please let us know if you would like to be a part of that beta program.

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

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer

Hi @SourabhBagrecha,

Sure, It would be great.

Thanks,
Saurabh Burman