- Give the number of films that each theatre is presenting. Hint: You need a reduce here.
// Map
function(doc) {
if(doc.feed.theaterShowtimes[0]) {
var movieTheater = doc.feed.theaterShowtimes[0].place.theater;
var moviesOnShow = doc.feed.theaterShowtimes[0].movieShowtimes;
for(var i=0; i < moviesOnShow.length; i++) {
emit(movieTheater.name, 1);
} // for
} // if
} // func
// Reduce
function (key, values) {
return sum(values) ;
}
- Give the list of films with a press rating higher than 4 stars. Attention: filter duplicates.
// Map
function(doc) {
if(doc.feed.theaterShowtimes[0]) {
var movies = doc.feed.theaterShowtimes[0].movieShowtimes;
for(var i=0; i < movies.length; i++) {
var movie = movies[i].onShow.movie;
if(movie.statistics.pressRating > 4) {
emit([movie.title, movie.statistics.pressRating], null);
}
} // for
} // if
} // func
// Reduce
function (keys,values) {
return null ;
}
- Give the list of films presented 2 years ago (10.12.2011), and for each film, the theatre where it was presented and its schedule.
function(doc) {
if(doc.feed.theaterShowtimes[0]) {
var movieTheater = doc.feed.theaterShowtimes[0].place.theater;
var moviesOnShow = doc.feed.theaterShowtimes[0].movieShowtimes;
for(var i=0; i < moviesOnShow.length; i++) {
var movie = {
"title": moviesOnShow[i].onShow.movie.title,
"theater": movieTheater.name,
"date": moviesOnShow[i].scr[0].d,
"schedule": []
};
if(movie.date == "2011-12-09") {
var showTime = moviesOnShow[i].scr[0].t;
for(var j=0; j < showTime.length; j++) {
movie.schedule.push( showTime[j].$ );
}
emit(movie.title, movie);
} // if
} // for
} // if
} // func
- BONUS! Give the list of films, and for every film, the list of theatres that present it (this question is a challenge but we encourage you to try to solve it).
// Map
function(doc) {
if(doc.feed.theaterShowtimes[0]) {
var movieTheater = doc.feed.theaterShowtimes[0].place.theater;
var moviesOnShow = doc.feed.theaterShowtimes[0].movieShowtimes;
for(var i=0; i < moviesOnShow.length; i++) {
var movie = moviesOnShow[i].onShow.movie;
emit(movie.title, movieTheater);
} // for
} // if
} // func
// Reduce
function(keys, values) {
var movieTheaters = [] ;
for(var i=0; i<values.length; i++) {
var theater = values[i].name;
if(!contains(movieTheaters, theater)) {
movieTheaters.push(theater);
}
} // for
return [ movieTheaters.length, movieTheaters ];
} // func
function contains(array, element) {
var isContained = false;
for(var i=0; i<array.length; i++) {
if(array[i] == element){
isContained = true;
break;
}
} // for
return isContained;
} // func