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

AGGREGATE Movies Tutorial

From NoSQLZoo
Revision as of 00:33, 15 October 2016 by Andr3w (talk | contribs) (→‎Casablanca)
Jump to: navigation, search

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 of the Tom Hanks movies.
db.movies.aggregate([
    {$match:{
        cast:'Harrison Ford'
    }},
    {$project:{_id:0,title:1}
]).pretty()
db.movies.aggregate([
    {$match:{
        cast:'Tom Hanks'
    }},
    {$project:{_id:0,title:1}
]).pretty()