Chapter 2: Basic Aggregation - Utility Stages

This is my statement but I don’t get a response similar to the options shown

:anguished:

db.movies.aggregate([
… {"$addFields": {
… commonToBoth: { $setIntersection: [ “$cast”, [ “Sandra Bullock”, “Tom Hanks”, “Julia Roberts”, “Kevin Spacey”, “George Clooney”]] }
… }},
… {"$project": {
… “_id”: 0,
… “title”: 1,
… “countries”:1,
… “imdb.rating”:1,
… “cast”: 1,
… num_coun: { $cond: { if: { $isArray: “$countries” }, then: { $size: “$countries” }, else: 0} },
… num_favs: { $cond: { if: { $isArray: “$commonToBoth” }, then: { $size: “$commonToBoth” }, else: 0} }
… }
… },
… {"$sort": {“num_favs”: -1 , “imdb.rating”: -1, “title”: -1, }
… },
… {"$match": {$and: [
… {“num_coun”: { “$eq”: 1}},
… {“countries”: { “$eq”: “USA”}},
… {“imdb.rating”: { “$gte”: 3}} ]}
… },
… { “$limit”: 25 }
… ])

{ “title” : “A Time to Kill”, “cast” : [ “Matthew McConaughey”, “Sandra Bullock”, “Samuel L. Jackson”, “Kevin Spacey” ], “imdb” : { “rating” : 7.4 }, “countries” : [ “USA” ], “num_coun” : 1, “num_favs” : 2 }
{ “title” : “Extremely Loud & Incredibly Close”, “cast” : [ “Tom Hanks”, “Thomas Horn”, “Sandra Bullock”, “Zoe Caldwell” ], “imdb” : { “rating” : 6.9 }, “countries” : [ “USA” ], “num_coun” : 1, “num_favs” : 2 }
{ “title” : “The War”, “cast” : [ “Keith David”, “Katharine Phillips”, “Tom Hanks”, “Paul Fussell” ], “imdb” : { “rating” : 9 }, “countries” : [ “USA” ], “num_coun” : 1, “num_favs” : 1 }
{ “title” : “From the Earth to the Moon”, “cast” : [ “Tom Hanks”, “Nick Searcy”, “Lane Smith”, “David Andrews” ], “imdb” : { “rating” : 8.9 }, “countries” : [ “USA” ], “num_coun” : 1, “num_favs” : 1 }
{ “title” : “Forrest Gump”, “cast” : [ “Tom Hanks”, “Rebecca Williams”, “Sally Field”, “Michael Conner Humphreys” ], “imdb” : { “rating” : 8.8 }, “countries” : [ “USA” ], “num_coun” : 1, “num_favs” : 1 }
{ “title” : “Saving Private Ryan”, “cast” : [ “Tom Hanks”, “Tom Sizemore”, “Edward Burns”, “Barry Pepper” ], “imdb” : { “rating” : 8.6 }, “countries” : [ “USA” ], “num_coun” : 1, “num_favs” : 1 }
{ “title” : “The Green Mile”, “cast” : [ “Tom Hanks”, “David Morse”, “Michael Clarke Duncan”, “Bonnie Hunt” ], “imdb” : { “rating” : 8.5 }, “countries” : [ “USA” ], “num_coun” : 1, “num_favs” : 1 }
{ “title” : “Our Brand Is Crisis”, “cast” : [ “Sandra Bullock”, “Billy Bob Thornton”, “Anthony Mackie”, “Zoe Kazan” ], “imdb” : { “rating” : 8.5 }, “countries” : [ “USA” ], “num_coun” : 1, “num_favs” : 1 }
{ “title” : “Toy Story 3”, “cast” : [ “Tom Hanks”, “Tim Allen”, “Joan Cusack”, “Ned Beatty” ], “imdb” : { “rating” : 8.4 }, “countries” : [ “USA” ], “num_coun” : 1, “num_favs” : 1 }
{ “title” : “American Beauty”, “cast” : [ “Kevin Spacey”, “Annette Bening”, “Thora Birch”, “Wes Bentley” ], “imdb” : { “rating” : 8.4 }, “countries” : [ “USA” ], “num_coun” : 1, “num_favs” : 1 }
{ “title” : “Toy Story”, “cast” : [ “Tom Hanks”, “Tim Allen”, “Don Rickles”, “Jim Varney” ], “imdb” : { “rating” : 8.3 }, “countries” : [ “USA” ], “num_coun” : 1, “num_favs” : 1 }
{ “title” : “L.A. Confidential”, “cast” : [ “Kevin Spacey”, “Russell Crowe”, “Guy Pearce”, “James Cromwell” ], “imdb” : { “rating” : 8.3 }, “countries” : [ “USA” ], “num_coun” : 1, “num_favs” : 1 }
{ “title” : “Look, Up in the Sky! The Amazing Story of Superman”, “cast” : [ “Kevin Spacey”, “Gerard Jones”, “Forrest J. Ackerman”, “Elliot S. Maggin” ], “imdb” : { “rating” : 8.1 }, “countries” : [ “USA” ], “num_coun” : 1, “num_favs” : 1 }
{ “title” : “Beslan: Three Days in September”, “cast” : [ “Ruslan Aushev”, “Shamil Basayev”, “Dmitry Beliakov”, “Julia Roberts” ], “imdb” : { “rating” : 8.1 }, “countries” : [ “USA” ], “num_coun” : 1, “num_favs” : 1 }
{ “title” : “Toy Story 2”, “cast” : [ “Tom Hanks”, “Tim Allen”, “Joan Cusack”, “Kelsey Grammer” ], “imdb” : { “rating” : 7.9 }, “countries” : [ “USA” ], “num_coun” : 1, “num_favs” : 1 }
{ “title” : “Shooting War”, “cast” : [ “Tom Hanks”, “Stephen Ambrose”, “Russ Meyer”, “Walter Rosenblum” ], “imdb” : { “rating” : 7.9 }, “countries” : [ “USA” ], “num_coun” : 1, “num_favs” : 1 }
{ “title” : “Captain Phillips”, “cast” : [ “Tom Hanks”, “Catherine Keener”, “Barkhad Abdi”, “Barkhad Abdirahman” ], “imdb” : { “rating” : 7.9 }, “countries” : [ “USA” ], “num_coun” : 1, “num_favs” : 1 }
{ “title” : “Shackleton’s Antarctic Adventure”, “cast” : [ “Kevin Spacey”, “Conrad Anker”, “Iain Fraser”, “Michael Gambon” ], “imdb” : { “rating” : 7.8 }, “countries” : [ “USA” ], “num_coun” : 1, “num_favs” : 1 }
{ “title” : “Ocean’s Eleven”, “cast” : [ “George Clooney”, “Cecelia Ann Birt”, “Paul L. Nolan”, “Carol Florence” ], “imdb” : { “rating” : 7.8 }, “countries” : [ “USA” ], “num_coun” : 1, “num_favs” : 1 }
{ “title” : “Fantastic Mr. Fox”, “cast” : [ “George Clooney”, “Meryl Streep”, “Jason Schwartzman”, “Bill Murray” ], “imdb” : { “rating” : 7.8 }, “countries” : [ “USA” ], “num_coun” : 1, “num_favs” : 1 }
Type “it” for more
MongoDB Enterprise Cluster0-shard-0:PRIMARY> it
{ “title” : “Toy Story Toons: Partysaurus Rex”, “cast” : [ “Tom Hanks”, “Tim Allen”, “Wallace Shawn”, “Corey Burton” ], “imdb” : { “rating” : 7.7 }, “countries” : [ “USA” ], “num_coun” : 1, “num_favs” : 1 }
{ “title” : “The Blind Side”, “cast” : [ “Sandra Bullock”, “Tim McGraw”, “Quinton Aaron”, “Jae Head” ], “imdb” : { “rating” : 7.7 }, “countries” : [ “USA” ], “num_coun” : 1, “num_favs” : 1 }
{ “title” : “Philadelphia”, “cast” : [ “Tom Hanks”, “Denzel Washington”, “Roberta Maxwell”, “Buzz Kilman” ], “imdb” : { “rating” : 7.7 }, “countries” : [ “USA” ], “num_coun” : 1, “num_favs” : 1 }
{ “title” : “Toy Story of Terror”, “cast” : [ “Tom Hanks”, “Tim Allen”, “Joan Cusack”, “Carl Weathers” ], “imdb” : { “rating” : 7.6 }, “countries” : [ “USA” ], “num_coun” : 1, “num_favs” : 1 }
{ “title” : “Toy Story Toons: Hawaiian Vacation”, “cast” : [ “Tom Hanks”, “Tim Allen”, “Joan Cusack”, “Kristen Schaal” ], “imdb” : { “rating” : 7.6 }, “countries” : [ “USA” ], “num_coun” : 1, “num_favs” : 1 }
MongoDB Enterprise Cluster0-shard-0:PRIMARY>

Hi Veronica,

Could you please tell me what exactly is the problem statement that you are trying to solve?

try moving the sort and limit, outside something like this and please check for the brackets

db.movies.aggregate([{"$addFields": {commonToBoth: { $setIntersection: [ “$cast”, [ “Sandra Bullock”, “Tom Hanks”, “Julia Roberts”, “Kevin Spacey”, “George Clooney”]] }
}},{"$project": {
“_id”: 0,
“title”: 1,
“countries”:1,
“imdb.rating”:1,
“cast”: 1,
num_coun: { $cond: { if: { $isArray: “$countries” }, then: { $size: “$countries” }, else: 0} },
num_favs: { $cond: { if: { $isArray: “$commonToBoth” }, then: { $size: “$commonToBoth” }, else: 0} }
}}, {"$match": {$and: [
{“num_coun”: { “$eq”: 1}},
{“countries”: { “$eq”: “USA”}},
{“imdb.rating”: { “$gte”: 3}} ]}
}]).sort({“num_favs”: -1 , “imdb.rating”: -1, “title”: -1}).limit(25)

I do not think that sort() or limit() will work after aggregate(). I will you will get not a function or something in that effect.

However, leaving them out until the rest of the pipeline works is a good idea.

Another think, the $and is superfluous as there is an implicit and with the $match.

Lastly, performance wise it is best to do $match as soon as possible. This way you filter out of the pipeline all useless documents and prevent doing $addFields and $project on documents that will be eliminated anyway.

I am trying something like this myself, but when I use this line in Compass

It says it is not properly formatted. Even though I directly copy pasted it.

Depending from where you copied it, you may end up with the wrong type of quotes.

If the code snippet that you copy, specially from web pages, is not in a properly code, pre or triple back ticks section you will end up with the fancy open and close matching single or double quotes.

See the differences below where the first is in a proper block and the second where it is not.

num_favs:
{
  $cond:
  {
    if: { $isArray: "$commonToBoth"},
    then: { $size: "$commonToBoth" },
    else: 0
  }
}

{
$cond:
{
if: { $isArray: “$commonToBoth”},
then: { $size: “$commonToBoth” },
else: 0
}
}

Thank you. That solved my problem, but seeing as this is not originally my thread, I do not have the “solution” box.