Working through this lab. Here is the pseudocode I wrote with my approach to the problem:
- $match tomatoes.viewer.rating = greater than 3 and releaed in USA
- Add variable called num_favs
- If Sandra Bullock is in cast add 1 to num_favs
- If Tom Hanks is in cast add 1 to num_favs
- If Kevin Spacey is in cast add 1 to num_favs
- If George Clooney is in cast add 1 to num_favs
- If Julia Roberts is in cast add 1 to num_favs
- Sort results by num_favs then tomatoes.viewer.rating then title
Based on that, I wrote this aggregation pipeline language:
db.movies.aggregate([
{$match: [{$tomatoes.viewer.rating: {$gt: 3}}, {countries: {$in: ["USA"]}}]},
{$project: {"_id": 0, "num_favs": 1}},
{$cond: {if: {cast: {$in: "Sandra Bullock"}}, then: {$inc: {"$num_fav": 1}}}},
{$cond: {if: {cast: {$in: "Tom Hanks"}}, then: {$inc: {"$num_fav": 1}}}},
{$cond: {if: {cast: {$in: "Kevin Spacey"}}, then: {$inc: {"$num_fav": 1}}}},
{$cond: {if: {cast: {$in: "George Clooney"}}, then: {$inc: {"$num_fav": 1}}}},
{$cond: {if: {cast: {$in: "Julia Roberts"}}, then: {$inc: {"$num_fav": 1}}}},
{$sort: { "num_fav": -1, "tomatoes.viewer.rating": -1, "title": -1 }}
])
When I run it in my terminal, I get this error: “uncaught exception: SyntaxError: missing : after property id :
@(shell):1:41”
Any idea what I’m doing wrong here?