MAPREDUCE tutorial

From NoSQLzoo
Jump to: navigation, search

This tutorial introduces the MapReduce command.

MapReduce examples are available.

Population of continents

Find the total population of the each continent

db.world.mapReduce(
   function(){emit(1,this.population);},
   function(k,v){return Array.sum(v);},
   {out:{inline:1}}
)
db.world.mapReduce(
   function(){emit(this.continent,this.population);},
   function(k,v){return Array.sum(v);},
   {out:{inline:1}}
)

Population of the world

Use the previous answer to find the population of the world.

You should emit the same key for every country - that way there will be only one value to reduce.

db.world.mapReduce(
   function(){emit(1,1);},
   function(k,v){return Array.sum(v);},
   {out:{inline:1}}
)
db.world.mapReduce(
   function(){emit(1,this.population);},
   function(k,v){return Array.sum(v);},
   {out:{inline:1}}
)

Countries per continent

Show the number of countries on each continent

You can emit the key value pair (this.continent,1) to group the results by continent and ensure countries are counted.

db.world.mapReduce(
   function(){emit(1,1);},
   function(k,v){return Array.sum(v);},
   {out:{inline:1}}
)
db.world.mapReduce(
   function(){emit(this.continent,1);},
   function(k,v){return Array.sum(v);},
   {out:{inline:1}}
)

By first letter

Count number of countries by first letter

The key to emit is the first character of the name of the country. In JavaScript you can treat a string as an array of characters so this.name[0] gives the first character.

db.world.mapReduce(
   function(){emit(1,1);},
   function(k,v){return Array.sum(v);},
   {out:{inline:1}}
)
db.world.mapReduce(
   function(){emit(this.name[0],1);},
   function(k,v){return Array.sum(v);},
   {out:{inline:1}}
)

How many large countries

Show how many countries have a population of more than 100 million.

You can protect the emit with an if statement. Only emit if the population passes the test (this.population>100000000)

db.world.mapReduce(
   function(){emit(1,1);},
   function(k,v){return Array.sum(v);},
   {out:{inline:1}}
)
db.world.mapReduce(
   function(){
     if (this.population>100000000)
       emit(1,1);
   },
   function(k,v){return Array.sum(v);},
   {out:{inline:1}}
)

First in Region

Return the first country based on name order for each continent

db.world.mapReduce(
   function(){emit(1,1);},
   function(k,v){return Array.sum(v);},
   {out:{inline:1}}
)
db.world.mapReduce(
  function(){emit(this.continent, this.name)},
  function(k, v){
    return v[0];
    },
    {out:{inline:1}}
)

Name or Capital starts with M

Return the number of countries where the name or capital city that starts with a letter 'M'

db.world.mapReduce(
   function(){emit(1,1);},
   function(k,v){return Array.sum(v);},
   {out:{inline:1}}
)
db.world.mapReduce(
   function(){
     if (this.name[0]=='M' || this.capital[0]=='M')
       emit(1,1);
   },
   function(k,v){return Array.sum(v);},
   {out:{inline:1}}
)

Name or Capital starts with M

Return the number of countries where the name or capital city that starts with a letter 'M'. Count a country twice if both are true.

db.world.mapReduce(
   function(){emit(1,1);},
   function(k,v){return Array.sum(v);},
   {out:{inline:1}}
)
db.world.mapReduce(
   function(){
     if (this.name[0]=='M')
       emit(1,1);
     if (this.capital[0]=='M')
       emit(1,1);
   },
   function(k,v){return Array.sum(v);},
   {out:{inline:1}}
)

First capital by continent

Show the first capital alphabetically for each continent

You can sort the list in the reduce function - then pick the first one. v.sort()[0]

db.world.mapReduce(
   function(){emit(1,1);},
   function(k,v){return Array.sum(v);},
   {out:{inline:1}}
)
db.world.mapReduce(
   function(){
       emit(this.continent,this.capital);
   },
   function(k,v){return v.sort()[0];},
   {out:{inline:1}}
)

Count sizes

Show country count for countries with populations in the ranges

 0 to 999,999
 1,000,000 to 499,999,999
 500,000,000 or more
db.world.mapReduce(
   function(){emit(1,1);},
   function(k,v){return Array.sum(v);},
   {out:{inline:1}}
)
db.world.mapReduce(
  function(){
                  var pop = this.population;
                  switch(true){
                    case pop<1000000:
                       emit("0 TO 999,999", 1);
                       break;
                    case pop<500000000:
                       emit("1,000,000 TO 499,999,999", 1);
                       break;
                    case pop>499999999:
                       emit("500,000,000 OR MORE", 1);
                       break;
                  }
 },
 function(key, values){ return Array.sum(values); },
 {out:{"inline":1}}
)