Cookies help us deliver our services. By using our services, you agree to our use of cookies. More information

Difference between revisions of "AGGREGATE Movies Tutorial"

From NoSQLZoo
Jump to: navigation, search
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
==Investigating the Movie Database==
 
==Investigating the Movie Database==
 
The movie database includes thousands of movies with documents such as:
 
The movie database includes thousands of movies with documents such as:
{
+
<syntaxhighlight lang="JavaScript">
"_id" : 10522,
+
{
"title" : "Alien",
+
    "_id" : 10522,
"yr" : 1979,
+
    "title" : "Alien",
"director" : "Ridley Scott",
+
    "yr" : 1979,
"budget" : 11000000,
+
    "director" : "Ridley Scott",
"gross" : 104931801,
+
    "budget" : 11000000,
"cast" : [
+
    "gross" : 104931801,
"Tom Skerritt",
+
    "cast" : [
"Sigourney Weaver",
+
        "Tom Skerritt",
"Veronica Cartwright",
+
        "Sigourney Weaver",
"Harry Dean Stanton",
+
        "Veronica Cartwright",
"John Hurt",
+
        "Harry Dean Stanton",
"Ian Holm",
+
        "John Hurt",
"Yaphet Kotto"
+
        "Ian Holm",
]
+
        "Yaphet Kotto"
}
+
    ]
 
+
}
 +
</syntaxhighlight>
 
==Casablanca==
 
==Casablanca==
<div class=q data-lang="mongo">
+
<div class="q" data-lang="mongo">
Show the details of the movie Casablanca
+
<p class="strong">Show the details of the movie Casablanca.</p>
<pre class=def>
+
<pre class="def"><nowiki>
db.movies.aggregate([
+
db.movies.aggregate([{
     {$match:{
+
     $match: {
         title:'Alien'
+
         title: 'Alien'
     }},
+
     }
]).pretty()
+
}]).pretty();</nowiki></pre>
</pre>
+
<pre class="ans"><nowiki>db.movies.aggregate([{$match:{title:'Casablanca'}}]).pretty(); </nowiki></pre>
<pre class=ans>db.movies.aggregate([
 
    {$match:{  
 
        title:'Casablanca'
 
    }}
 
]).pretty()
 
</pre>
 
 
</div>
 
</div>
  
 
==Tom Hanks==
 
==Tom Hanks==
<div class=q data-lang="mongo">
+
<div class="q" data-lang="mongo">
 
You can use the match operator on listed items like cast. As there is an index on cast these queries operate quickly and do not have to scan the entire collection.
 
You can use the match operator on listed items like cast. As there is an index on cast these queries operate quickly and do not have to scan the entire collection.
<div class=i>Show the title and year of the 10 most recent Tom Hanks movies - show the most recent first.</div>
+
<p class="strong">Show the title and year of the 10 most recent Tom Hanks movies - show the most recent first.</p>
<pre class=def>
+
<pre class="def"><nowiki>
 
db.movies.aggregate([
 
db.movies.aggregate([
     {$match:{
+
     {$match: {
         cast:'Harrison Ford'
+
         cast: 'Harrison Ford'
 
     }},
 
     }},
     {$project:{_id:0,title:1}}
+
     {$project: {_id: 0, title: 1}}
]).pretty()
+
]).pretty();</nowiki></pre>
</pre>
+
<pre class="ans">db.movies.aggregate([{$match:{cast:'Tom Hanks'}},{$project:{title:1,yr:1,_id:0}},{$sort:{yr:-1}},{$limit:10}]).pretty()</pre>
<pre class=ans>
+
</div>
 +
 
 +
==Brad and George==
 +
<div class="q" data-lang="mongo">
 +
<p class="strong">Show the movies that included Brad Pitt and George Clooney. Give the title and year ordered by year</p>
 +
<pre class="def"><nowiki>
 
db.movies.aggregate([
 
db.movies.aggregate([
     {$match:{
+
     {$match: {
         cast:'Tom Hanks'
+
         cast: 'Harrison Ford'
 
     }},
 
     }},
     {$project:{title:1,yr:1,_id:0}},
+
     {$project: {_id: 0, title: 1}}
    {$sort:{yr:-1}},
+
]).pretty();</nowiki></pre>
    {$limit:10}
+
<pre class="ans">db.movies.aggregate([{$match:{cast:'Brad Pitt'}},{$match:{cast:'George Clooney'}},{$project:{_id:0,title:1,yr:1}},{$sort:{yr:1}}]).pretty()</pre>
])
 
</pre>
 
 
</div>
 
</div>
  
 
==Dark Years==
 
==Dark Years==
<div class=q data-lang="mongo">
+
<div class="q" data-lang="mongo">
A "Dark Year" is one in which Jack Black made more than two movies.
+
A "Dark Year" is one in which '''Jack Black''' made more than two movies.
<div class=i>List the years in which Jack Black made three or more movies.</div>
+
<p class="strong">List the years in which Jack Black made three or more movies.</p>
<div class=hint>
+
<div class="hint">
One way to do this is to set up an aggregate pipeline with the following stages:
+
One way to do this is to set up an aggregate pipeline with the following stages. You can build up to your answer by completing each stage in turn:
*Use $match to select the Jack Black movies
+
*Use '''$match''' to select the '''Jack Black''' movies - this will include howlers such as "'''Anchorman: The Legend of Ron Burgundy'''" (2004) and "'''School of Rock'''" (2003).
*Use $group together with $sum:1 to get the years together with a count, you will have to name the count field
+
*Use '''$group''' together with <syntaxhighlight lang="JavaScript" inline>$sum: 1</syntaxhighlight> to get the years together with a count, you will have to name the count field
*Use $match to get those where the count is $gte:3
+
*Use '''$match''' to get those where the count is <syntaxhighlight lang="JavaScript" inline>$gte: 3</syntaxhighlight>.
*Use $project to extract the year only
+
*Use '''$project''' to extract the year only.
*Use sort to get the output in ascending order
+
*Use '''$sort''' to get the output in ascending order.
 
</div>
 
</div>
<pre class=def>
+
<pre class="def"><nowiki>
db.movies.aggregate([
 
    {$match:{
 
        cast:'Harrison Ford'
 
    }},
 
    {$project:{_id:0,title:1}}
 
]).pretty()
 
</pre>
 
<pre class=ans>
 
 
db.movies.aggregate([
 
db.movies.aggregate([
     {$match:{
+
     {$match: {
         cast:'Jack Black'
+
         cast: 'Harrison Ford'
 
     }},
 
     }},
     {$group:{_id:'$yr',num:{$sum:1}}},
+
     {$project: {_id: 0, title: 1}}
    {$match:{num:{$gte:3}}},
+
]).pretty();</nowiki></pre>
    {$project:{_id:1}},
+
<pre class="ans"><nowiki>db.movies.aggregate([{$match:{cast:'Jack Black'}},{$group:{_id:'$yr',num:{$sum:1}}},{$match:{num:{$gte:3}}},{$project:{_id:1}},{$sort:{_id:1}}]).pretty();</nowiki></pre>
    {$sort:{_id:1}}
 
]).pretty()
 
</pre>
 
 
</div>
 
</div>

Latest revision as of 22:58, 31 March 2021

Investigating the Movie Database

The movie database includes thousands of movies with documents such as:

{
     "_id" : 10522,
     "title" : "Alien",
     "yr" : 1979,
     "director" : "Ridley Scott",
     "budget" : 11000000,
     "gross" : 104931801,
     "cast" : [
         "Tom Skerritt",
         "Sigourney Weaver",
         "Veronica Cartwright",
         "Harry Dean Stanton",
         "John Hurt",
         "Ian Holm",
         "Yaphet Kotto"
     ]
}

Casablanca

Show the details of the movie Casablanca.

db.movies.aggregate([{
    $match: {
        title: 'Alien'
    }
}]).pretty();
db.movies.aggregate([{$match:{title:'Casablanca'}}]).pretty(); 

Tom Hanks

You can use the match operator on listed items like cast. As there is an index on cast these queries operate quickly and do not have to scan the entire collection.

Show the title and year of the 10 most recent Tom Hanks movies - show the most recent first.

db.movies.aggregate([
    {$match: {
        cast: 'Harrison Ford'
    }},
    {$project: {_id: 0, title: 1}}
]).pretty();
db.movies.aggregate([{$match:{cast:'Tom Hanks'}},{$project:{title:1,yr:1,_id:0}},{$sort:{yr:-1}},{$limit:10}]).pretty()

Brad and George

Show the movies that included Brad Pitt and George Clooney. Give the title and year ordered by year

db.movies.aggregate([
    {$match: {
        cast: 'Harrison Ford'
    }},
    {$project: {_id: 0, title: 1}}
]).pretty();
db.movies.aggregate([{$match:{cast:'Brad Pitt'}},{$match:{cast:'George Clooney'}},{$project:{_id:0,title:1,yr:1}},{$sort:{yr:1}}]).pretty()

Dark Years

A "Dark Year" is one in which Jack Black made more than two movies.

List the years in which Jack Black made three or more movies.

One way to do this is to set up an aggregate pipeline with the following stages. You can build up to your answer by completing each stage in turn:

  • Use $match to select the Jack Black movies - this will include howlers such as "Anchorman: The Legend of Ron Burgundy" (2004) and "School of Rock" (2003).
  • Use $group together with $sum: 1 to get the years together with a count, you will have to name the count field
  • Use $match to get those where the count is $gte: 3.
  • Use $project to extract the year only.
  • Use $sort to get the output in ascending order.
db.movies.aggregate([
    {$match: {
        cast: 'Harrison Ford'
    }},
    {$project: {_id: 0, title: 1}}
]).pretty();
db.movies.aggregate([{$match:{cast:'Jack Black'}},{$group:{_id:'$yr',num:{$sum:1}}},{$match:{num:{$gte:3}}},{$project:{_id:1}},{$sort:{_id:1}}]).pretty();