I was able to get the solution easy enough, I couldn’t get the $setIntersection to return the correct answer (obviously, “The Godfather” – and I disagree with the video that the Godfather II was better. Both were great, but the first was amazing…
I solved it this way after refactoring a few times, I found it to be the most efficient (which helps trying to use Compass).
db.movies.aggregate([
{ "$match": {
"imdb.rating": {"$gt": 0},
"metacritic": {$type: "int"}
}},
{"$project": {
_id: 0,
title: 1,
"imdb.rating": 1,
metacritic: 1
}},
{"$facet": {
"byImdbRating": [
{$sort: {"imdb.rating": -1}},
{$limit: 10},
{
"$bucketAuto": {
"groupBy": "$imdb.rating",
"buckets": 1,
"output": {
"titles": {"$push": "$title"}
}
}
},
],
"byMetacriticRating": [
{$sort: {metacritic: -1}},
{$limit: 10},
{
"$bucketAuto": {
"groupBy": "$metacritic",
"buckets": 1,
"output": {
"titles": {"$push": "$title"}
}
}
}
]
}},
{$project: {
"byMetacriticRating.titles":1,
"byImdbRating.titles":1,
}},
{$addFields: {
"commonToBoth": {$setIntersection: ["$byMetacriticRating.titles", "$byImdbRating.titles"]}
}}
]).pretty()
My result set is as follows:
{
"byImdbRating" : [
{
"titles" : [
"The Power of Nightmares: The Rise of the Politics of Fear",
"Pulp Fiction",
"Schindler's List",
"The Good, the Bad and the Ugly",
"The Century of the Self",
"The Dark Knight",
"The Godfather: Part II",
"The Martian",
"The Godfather",
"The Shawshank Redemption"
]
}
],
"byMetacriticRating" : [
{
"titles" : [
"Boyhood",
"Fanny and Alexander",
"The Conformist",
"Lawrence of Arabia",
"Au Hasard Balthazar",
"Sweet Smell of Success",
"The Leopard",
"The Godfather",
"Journey to Italy",
"Best Kept Secret"
]
}
],
"commonToBoth" : [ ]
}
I tried a bunch of different things to get that final thing working, I just didn’t have time to Trial&Error every Aggregation operator, so what step did I miss? I suspected it was something to do with the fact that my results were an object within an array, but curious…