Difference between revisions of "AGGREGATE Movies Tutorial"
From NoSQLZoo
Line 40: | Line 40: | ||
<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 of the Tom Hanks movies.</div> | + | <div class=i>Show the title and year of the 10 most recent Tom Hanks movies - show the most recent first.</div> |
<pre class=def> | <pre class=def> | ||
db.movies.aggregate([ | db.movies.aggregate([ | ||
Line 54: | Line 54: | ||
cast:'Tom Hanks' | cast:'Tom Hanks' | ||
}}, | }}, | ||
− | {$project:{_id:0, | + | {$project:{title:1,yr:1,_id:0}}, |
− | ] | + | {$sort:{yr:-1}}, |
+ | {$limit:10} | ||
+ | ]) | ||
</pre> | </pre> | ||
</div> | </div> |
Revision as of 23:41, 14 October 2016
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} ])