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

Difference between revisions of "AGGREGATE Tutorial"

From NoSQLZoo
Jump to: navigation, search
Line 5: Line 5:
 
<div class='extra_space' style='width:1em; height:6em;'></div>
 
<div class='extra_space' style='width:1em; height:6em;'></div>
 
==Per Capita GDP==
 
==Per Capita GDP==
<div class=q data-lang="mongo">
+
<div class="q" data-lang="mongo">
 
Give the <code>name</code> and the <code>per capita GDP</code> for those countries with a <code>population</code> of at least 200 million.
 
Give the <code>name</code> and the <code>per capita GDP</code> for those countries with a <code>population</code> of at least 200 million.
 
<div class="hint" title="How to calculate per capita GDP">
 
<div class="hint" title="How to calculate per capita GDP">
 
per capita GDP is the GDP divided by the population.
 
per capita GDP is the GDP divided by the population.
 
</div>
 
</div>
<pre class=def>
+
<pre class="def"><nowiki>
 
db.world.aggregate([
 
db.world.aggregate([
 
     {$match:{
 
     {$match:{
Line 21: Line 21:
 
     }}
 
     }}
 
])
 
])
</pre>
+
</nowiki></pre>
<div class=ans>db.world.aggregate([{"$match":{"population":{"$gte":200000000}}},{"$project":{"_id":0,"name":1,"per capita GDP": {"$divide": ["$gdp","$population"]}}}])
+
<pre class="ans"><nowiki>db.world.aggregate([{"$match":{"population":{"$gte":200000000}}},{"$project":{"_id":0,"name":1,"per capita GDP": {"$divide": ["$gdp","$population"]}}}])
</div>
+
</nowiki></pre>
 
</div>
 
</div>
 
==Population Density in South America==
 
==Population Density in South America==
<div class=q data-lang="mongo">
+
<div class="q" data-lang="mongo">
 
Give the <code>name</code> and the <code>population density</code> of all countries in South America.
 
Give the <code>name</code> and the <code>population density</code> of all countries in South America.
 
<div class="hint" title="How to calculate population density">
 
<div class="hint" title="How to calculate population density">
Line 34: Line 34:
 
Use a <code>$match</code>. <code>{"area":{"$ne":0}}</code>
 
Use a <code>$match</code>. <code>{"area":{"$ne":0}}</code>
 
</div>
 
</div>
<pre class=def>
+
<pre class="def"><nowiki>
 
db.world.aggregate([
 
db.world.aggregate([
 
     {$match:{continent:'Asia'}},
 
     {$match:{continent:'Asia'}},
Line 43: Line 43:
 
     }},
 
     }},
 
])
 
])
</pre>
+
</nowiki></pre>
<div class=ans>
+
<pre class="ans"><nowiki>
db.world.aggregate([
+
db.world.aggregate([{$match:{continent:'South America'}},{$project:{_id:0,name:1,density:{$divide:["$population","$area"]}}}])
    {$match:{continent:'South America'}},
+
</nowiki></pre>
    {$project:{
 
        _id:0,
 
        name:1,
 
        density: {$divide: ["$population","$area"]}
 
    }}
 
])
 
 
</div>
 
</div>
</div>
 
 
 
==Population Density for "V"==
 
==Population Density for "V"==
<div class=q data-lang="mongo">
+
<div class="q" data-lang="mongo">
 
Give the <code>name</code> and the <code>population density</code> of all countries with name after V in the alphabet.
 
Give the <code>name</code> and the <code>population density</code> of all countries with name after V in the alphabet.
 
<p>Note that because Vatican City (with area 0) is in Europe you will get a divide by zero error unless you filter first.
 
<p>Note that because Vatican City (with area 0) is in Europe you will get a divide by zero error unless you filter first.
Line 73: Line 65:
 
</pre>
 
</pre>
 
</div>
 
</div>
<pre class=def>
+
<pre class="def"><nowiki>
db.world.aggregate([
 
    {$match:{name:{$gt:'V'}}},
 
    {$project:{
 
        _id:0,
 
        name:1,
 
        area:1
 
    }},
 
])
 
</pre>
 
<div class=ans>
 
 
db.world.aggregate([
 
db.world.aggregate([
     {$match:{name:{$gt:'V'}}},
+
     {$match: {name: {$gt: 'V'}}},
    {$match:{area:{"$ne":0}}},
+
     {$project: {
     {$project:{
+
         _id: 0,
         _id:0,
+
         name: 1,
         name:1,
+
         area: 1
         density: {$divide: ["$population","$area"]}
 
 
     }}
 
     }}
 
])
 
])
 +
</nowiki></pre>
 +
<pre class="ans"><nowiki>
 +
db.world.aggregate([{$match:{name:{$gt:'V'}}},{$match:{area:{"$ne":0}}},{$project:{_id:0,name:1,density:{$divide:["$population","$area"]}}}])
 +
</nowiki></pre>
 
</div>
 
</div>
</div>
 
 
 
==Population in millions==
 
==Population in millions==
<div class=q data-lang="mongo">
+
<div class="q" data-lang="mongo">
 
Show the <code>name</code> and <code>population</code> in millions for the countries of the continent <b>South America</b>. Divide the population by 1000000 to get population in millions.  
 
Show the <code>name</code> and <code>population</code> in millions for the countries of the continent <b>South America</b>. Divide the population by 1000000 to get population in millions.  
<pre class=def>
+
<pre class="def"><nowiki>
 
db.world.aggregate([
 
db.world.aggregate([
 
     {$match:{
 
     {$match:{
Line 105: Line 88:
 
     }},
 
     }},
 
     {$project:{
 
     {$project:{
         _id:0,
+
         _id: 0,
         name:1
+
         name: 1
 
     }}
 
     }}
 
])
 
])
</pre>
+
</nowiki></pre>
<div class=ans>
+
<pre class="ans"><nowiki>
 
db.world.aggregate([{"$match":{"continent":{"$eq":"South America"}}},{"$project":{"_id":0,"name":1,"population":{"$divide":["$population",1000000]}}}])
 
db.world.aggregate([{"$match":{"continent":{"$eq":"South America"}}},{"$project":{"_id":0,"name":1,"population":{"$divide":["$population",1000000]}}}])
</div>
+
</nowiki></pre>
 
</div>
 
</div>
  
 
==Population density==
 
==Population density==
<div class=q data-lang="mongo">
+
<div class="q" data-lang="mongo">
 
Show the <code>name</code> and <code>population density</code> for <b>France</b>, <b>Germany</b>, and <b>Italy</b>
 
Show the <code>name</code> and <code>population density</code> for <b>France</b>, <b>Germany</b>, and <b>Italy</b>
<pre class=def>
+
<pre class="def"><nowiki>
 
db.world.aggregate([
 
db.world.aggregate([
 
     {$match:{
 
     {$match:{
         name: {$in:['United Kingdom','United States','Brazil']},
+
         name: {$in: ['United Kingdom', 'United States', 'Brazil']},
 
         population: {$ne: null},
 
         population: {$ne: null},
 
         area: {$ne: 0}
 
         area: {$ne: 0}
 
     }},
 
     }},
 
     {$project:{
 
     {$project:{
         _id:0,
+
         _id: 0,
         name:1
+
         name: 1
 
     }}
 
     }}
 
])
 
])
</pre>
+
</nowiki></pre>
<div class=ans>
+
<pre class="ans"><nowiki>
 
db.world.aggregate([{"$match":{"name":{"$in":['France','Germany','Italy']},"population":{"$ne":null},"area":{"$ne":0}}},{"$project":{"_id":0,"name":1,"population density":{"$divide":["$population","$area"]}}}])
 
db.world.aggregate([{"$match":{"name":{"$in":['France','Germany','Italy']},"population":{"$ne":null},"area":{"$ne":0}}},{"$project":{"_id":0,"name":1,"population density":{"$divide":["$population","$area"]}}}])
 +
</nowiki></pre>
 
</div>
 
</div>
</div>
 
 
 
==Continents by area==
 
==Continents by area==
<div class=q data-lang="mongo">
+
<div class="q" data-lang="mongo">
 
Order the <code>continents</code> by <code>area</code> from most to least.
 
Order the <code>continents</code> by <code>area</code> from most to least.
<pre class=def>
+
<pre class="def"><nowiki>
 
db.world.aggregate([
 
db.world.aggregate([
     {$group:{
+
     {$group: {
         _id:"$name",
+
         _id: "$name",
         area:{$max: "$area"}
+
         area: {$max: "$area"}
 
     }},
 
     }},
     {$sort:{
+
     {$sort: {
 
         area: -1
 
         area: -1
 
     }},
 
     }},
     {$project:{
+
     {$project: {
         _id:1,
+
         _id: 1,
         area:1
+
         area: 1
 
     }}
 
     }}
 
])
 
])
</pre>
+
</nowiki></pre>
<div class=ans>
+
<pre class="ans"><nowiki>
    db.world.aggregate([
+
db.world.aggregate([{"$group":{"_id":"$continent","area":{"$sum":"$area"}}},{"$sort":{"area":-1}},{"$project":{"_id":1,"area":1}}])
        {"$group":{
+
</nowiki></pre>
            "_id":"$continent",
 
            "area":{"$sum": "$area"}
 
        }},
 
        {"$sort":{
 
            "area": -1
 
        }},
 
        {"$project":{
 
            "_id":1,
 
            "area":1
 
        }}
 
    ])
 
</div>
 
 
</div>
 
</div>
  
 
==Big Continents==
 
==Big Continents==
<div class=q data-lang="mongo">
+
<div class="q" data-lang="mongo">
 
Show the only two continents with total area greater than 25000000 and then sort from largest to smallest.
 
Show the only two continents with total area greater than 25000000 and then sort from largest to smallest.
<pre class=def>
+
<pre class="def"><nowiki>
 
db.world.aggregate([
 
db.world.aggregate([
   {$match:{
+
   {$match: {
       continent:"North America"
+
       continent: "North America"
 
   }},
 
   }},
   {$project:{
+
   {$project: {
       _id:0,
+
       _id: 0,
       name:1
+
       name: 1
 
   }}
 
   }}
 
])
 
])
</pre>
+
</nowiki></pre>
<div class=ans>
+
<pre class="ans"><nowiki>
db.world.aggregate([
+
db.world.aggregate([{$group:{_id:"$continent",area:{$sum:"$area"}}},{$sort:{area:-1}},{$match:{area:{$gt:25000000}}}])
    {$group:{
+
</nowiki></pre>
        _id:"$continent",
 
        area:{$sum: "$area"}
 
    }},
 
    {$sort:{
 
        area: -1
 
    }},
 
    {$match:{
 
        area:{$gt:25000000}
 
    }}
 
])
 
 
</div>
 
</div>
</div>
 
 
 
==First and last country by continent==
 
==First and last country by continent==
<div class=q data-lang="mongo">
+
<div class="q" data-lang="mongo">
 
For each continent show the first and last country alphabetically like this:
 
For each continent show the first and last country alphabetically like this:
 
  { "_id" : "Africa", "from" : "Algeria", "to" : "Zimbabwe" }
 
  { "_id" : "Africa", "from" : "Algeria", "to" : "Zimbabwe" }
Line 212: Line 170:
 
  { "_id" : "Oceania", "from" : "Australia", "to" : "Vanuatu" }
 
  { "_id" : "Oceania", "from" : "Australia", "to" : "Vanuatu" }
 
  { "_id" : "South America", "from" : "Argentina", "to" : "Venezuela" }
 
  { "_id" : "South America", "from" : "Argentina", "to" : "Venezuela" }
<pre class=def>
+
<pre class="def"><nowiki>
 
db.world.aggregate([
 
db.world.aggregate([
 
   {$group:{
 
   {$group:{
Line 221: Line 179:
 
   }}
 
   }}
 
])
 
])
</pre>
+
</nowiki></pre>
<div class=ans>
+
<pre class="ans"><nowiki>
db.world.aggregate([
+
db.world.aggregate([{$sort:{name:1}},{$group:{_id:'$continent',from:{$first:'$name'},to:{$last:'$name'}},},{$sort:{_id:1}}])
    {$sort:{name:1}},
+
</nowiki></pre>
    {$group:{
 
      _id:'$continent',
 
      from:{$first:'$name'},
 
      to:{$last:'$name'}
 
      },
 
    },
 
    {$sort:{_id:1}}
 
])
 
 
</div>
 
</div>
</div>
 
 
 
==Countries beginning with...==
 
==Countries beginning with...==
<div class=q data-lang="mongo">
+
<div class="q" data-lang="mongo">
 
Group countries according to the first letter of the name. As shown.
 
Group countries according to the first letter of the name. As shown.
 
Only give "U" through to "Z".
 
Only give "U" through to "Z".
  
 
You will need to use the '''$substr''' function and the '''$push''' aggregate function.
 
You will need to use the '''$substr''' function and the '''$push''' aggregate function.
 +
<pre><nowiki>
 
  { "_id" : "U", "list" : [ "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "Uruguay", "Uzbekistan" ] }
 
  { "_id" : "U", "list" : [ "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "Uruguay", "Uzbekistan" ] }
 
  { "_id" : "V", "list" : [ "Vanuatu", "Vatican City", "Venezuela", "Vietnam" ] }
 
  { "_id" : "V", "list" : [ "Vanuatu", "Vatican City", "Venezuela", "Vietnam" ] }
 
  { "_id" : "Y", "list" : [ "Yemen" ] }
 
  { "_id" : "Y", "list" : [ "Yemen" ] }
 
  { "_id" : "Z", "list" : [ "Zambia", "Zimbabwe" ] }
 
  { "_id" : "Z", "list" : [ "Zambia", "Zimbabwe" ] }
<pre class=def>
+
</nowiki></pre>
 +
<pre class="def"><nowiki>
 
db.world.aggregate([
 
db.world.aggregate([
    {$project:{_id:'$name',
+
  {$project: {
    startsWith:{$substr:['$name',0,1]}}},
+
    _id: '$name',
    {$match:{_id:{$gte:'U'}}},
+
    startsWith: {$substr: ['$name', 0, 1]}
    {$sort:{_id:1}}
+
  }},  
])
+
  {$match: {
</pre>
+
     _id: {$gte: 'U'}
<div class=ans>
+
  }},
db.world.aggregate([
+
  {$sort: {_id: 1}}
     {$group:{
 
        _id:{ $substr:['$name',0,1] },
 
        list:{$push:'$name'}
 
    }},
 
    {$match:{_id:{$gte:'U'}}},
 
    {$sort:{_id:1}}
 
 
])
 
])
 +
</nowiki></pre>
 +
<pre class="ans"><nowiki>
 +
db.world.aggregate([{$group:{_id:{$substr:['$name',0,1]},list:{$push:'$name'}}},{$match:{_id:{$gte:'U'}}},{$sort:{_id:1}}])
 +
</nowiki></pre>
 
</div>
 
</div>
</div>
 
 
 
==Harder Questions==
 
==Harder Questions==
 
 
==Messing with continent names==
 
==Messing with continent names==
<div class=q data-lang="mongo">
+
<div class="q" data-lang="mongo">
 
Combine <b>North America</b> and <b>South America</b> to <b>America</b>, and then list the continents by area. Biggest first.
 
Combine <b>North America</b> and <b>South America</b> to <b>America</b>, and then list the continents by area. Biggest first.
<pre class=def>
+
<pre class="def"><nowiki>
 
db.world.aggregate([
 
db.world.aggregate([
    {$group:{
+
  {$group: {
        _id:{
+
    _id: {
            $cond: [{$eq:["$continent","North America"]},"America",
+
      $cond: [
                {$cond: [{$eq:["$continent","Asia"]},"The East","$continent"]}]
+
        {$eq: ["$continent", "North America"]},  
        },
+
        "America",
        area:{$sum: "$area"}
+
        {$cond: [
    }},
+
          {$eq: ["$continent", "Asia"]},
    {$sort:{
+
          "The East",
        area: -1
+
          "$continent"
    }},
+
        ]}
    {$project:{
+
      ]
        _id:1,
+
    },
        area:1
+
    area: {$sum: "$area"}
    }}
+
  }},  
 +
  {$sort: {area: -1}},
 +
  {$project: {
 +
    _id: 1,
 +
    area: 1
 +
  }}
 
])
 
])
</pre>
+
</nowiki></pre>
<div class=ans>
+
<pre class="ans"><nowiki>
 
db.world.aggregate([{"$group":{"_id":{"$cond":[{"$eq":["$continent","South America"]},"America",{"$cond":[{"$eq":["$continent","North America"]},"America","$continent"]}]},"area":{"$sum":"$area"}}},{"$sort":{"area":-1}},{"$project":{"_id":1,"area":1}}])  
 
db.world.aggregate([{"$group":{"_id":{"$cond":[{"$eq":["$continent","South America"]},"America",{"$cond":[{"$eq":["$continent","North America"]},"America","$continent"]}]},"area":{"$sum":"$area"}}},{"$sort":{"area":-1}},{"$project":{"_id":1,"area":1}}])  
</div>
+
</nowiki></pre>
 
</div>
 
</div>
  
 
==Messing with continent names 2==
 
==Messing with continent names 2==
<div class=q data-lang="mongo">
+
<div class="q" data-lang="mongo">
 
Show the <b>name</b> and the <b>continent</b> for countries beginning with N - but replace the continent <b>Oceania</b> with <b>Australasia</b>.
 
Show the <b>name</b> and the <b>continent</b> for countries beginning with N - but replace the continent <b>Oceania</b> with <b>Australasia</b>.
<pre class=def>
+
<pre class="def"><nowiki>
 
db.world.aggregate([
 
db.world.aggregate([
     {$match:{
+
     {$match: {
         name:{$regex:"^N"}
+
         name: {$regex: "^N"}
 
     }},
 
     }},
     {$project:{
+
     {$project: {
         _id:0,
+
         _id: 0,
         name:1
+
         name: 1
 
     }}
 
     }}
 
])
 
])
</pre>
+
</nowiki></pre>
<div class=ans>
+
<pre class="ans"><nowiki>
 
db.world.aggregate([{"$match":{"name":{"$regex":"^N"}}},{"$project":{"_id":0,"name":1,"continent":{"$cond":[{"$eq":["$continent","Oceania"]},"Australasia","$continent"]}}}])
 
db.world.aggregate([{"$match":{"name":{"$regex":"^N"}}},{"$project":{"_id":0,"name":1,"continent":{"$cond":[{"$eq":["$continent","Oceania"]},"Australasia","$continent"]}}}])
</div>
+
</nowiki></pre>
 
</div>
 
</div>
  
 
==Messing with continent names 3==
 
==Messing with continent names 3==
<div class=q data-lang="mongo">
+
<div class="q" data-lang="mongo">
 
Show the <b>name</b> and the <b>continent</b> but:<br/><br/>
 
Show the <b>name</b> and the <b>continent</b> but:<br/><br/>
&nbsp;&nbsp;&nbsp;&nbsp;substitute <b>Eurasia</b> for <b>Europe</b> and <b>Asia</b>.<br/>
+
*substitute <b>Eurasia</b> for <b>Europe</b> and <b>Asia</b>.<br/>
&nbsp;&nbsp;&nbsp;&nbsp;substitute <b>America</b> - for each country in <b>North America</b> or <b>South America</b> or <b>Caribbean</b>.<br/><br/>Only show countries beginning with <b>A</b> or <b>B</b><br/>
+
*substitute <b>America</b> - for each country in <b>North America</b> or <b>South America</b> or <b>Caribbean</b>.<br/><br/>Only show countries beginning with <b>A</b> or <b>B</b><br/>
 
If you're struggling you may want to experiment with <code>$and</code>,<code>$or</code>, etc.
 
If you're struggling you may want to experiment with <code>$and</code>,<code>$or</code>, etc.
<pre class=def></pre>
+
<pre class="def"></pre>
<div class=ans>
+
<pre class="ans"><nowiki>
db.world.aggregate([
+
db.world.aggregate([{$match:{name:{$regex:"^A|^B"}}},{$project:{_id:0,name:1,continent:{$cond:[{$or:[{$eq:["$continent","Europe"]},{$eq:["$continent","Asia"]}]},"Eurasia",{$cond:[{$or:[{$eq:["$continent","North America"]},{$eq:["$continent","South America"]},{$eq:["$continent","Caribbean"]}]},"America","$continent"]}]}}}])
    {$match:{
+
</nowiki></pre>
        name:{$regex:"^A|^B"}
 
    }},
 
    {$project:{
 
        _id:0,
 
        name:1,
 
        continent: {
 
            $cond: [{
 
                $or:[  
 
                    {$eq:["$continent","Europe"]},
 
                    {$eq:["$continent","Asia"]}
 
                ]},"Eurasia",{
 
                    $cond: [
 
                        {$or:[  
 
                            {$eq:["$continent","North America"]},
 
                            {$eq:["$continent","South America"]},
 
                            {$eq:["$continent","Caribbean"]}
 
                        ]},"America","$continent"]}
 
            ]}
 
    }}
 
])
 
</div>
 
 
</div>
 
</div>
  
 
==Messing with continent names 4==
 
==Messing with continent names 4==
<div class=q data-lang="mongo">
+
<div class="q" data-lang="mongo">
 
Put the continents right...
 
Put the continents right...
 
<br/><br/>
 
<br/><br/>
&nbsp;&nbsp;&nbsp;&nbsp;<b>Oceania</b> becomes <b>Australasia</b><br/>
+
*<b>Oceania</b> becomes <b>Australasia</b><br/>
&nbsp;&nbsp;&nbsp;&nbsp;Countries in <b>Eurasia</b> and <b>Turkey</b> go to <b>Europe/Asia</b><br/>
+
*Countries in <b>Eurasia</b> and <b>Turkey</b> go to <b>Europe/Asia</b><br/>
&nbsp;&nbsp;&nbsp;&nbsp;<b>Caribbean</b> islands starting with '<b>B</b>' go to <b>North America</b>, other Caribbean islands go to <b>South America</b><br/>
+
*<b>Caribbean</b> islands starting with '<b>B</b>' go to <b>North America</b>, other Caribbean islands go to <b>South America</b><br/>
 
<br/><br/>
 
<br/><br/>
 
<b>Show the name, the original continent and the new continent of all countries.</b>
 
<b>Show the name, the original continent and the new continent of all countries.</b>
<pre class=def></pre>
+
<pre class="def"></pre>
<div class=ans>
+
<pre class="ans"><nowiki>
    db.world.aggregate([
+
db.world.aggregate([{"$project":{"_id":0,"name":1,"original":"$continent","new":{"$cond":[{"$or":[{"$eq":["$continent","Eurasia"]},{"$eq":["$name","Turkey"]}]},"Europe/Asia",{"$cond":[{"$eq":["$continent","Oceania"]},"Australasia",{"$cond":[{"$and":[{"$eq":["$continent","Caribbean"]},{"$eq":[{"$substr":["$name",0,1]},"B"]}]},"North America",{"$cond":[{"$and":[{"$eq":["$continent","Caribbean"]},{"$ne":[{"$substr":["$name",0,1]},"B"]}]},"South America","$continent"]}]}]}]}}}])
        {"$project":{
+
</nowiki></pre>
            "_id":0,
 
            "name":1,
 
            "original": "$continent",
 
            "new": {
 
                "$cond": [
 
                    {"$or":[  
 
                        {"$eq":["$continent","Eurasia"]},
 
                        {"$eq":["$name","Turkey"]}
 
                    ]},"Europe/Asia",{
 
                        "$cond":[
 
                            {"$eq":["$continent","Oceania"]},"Australasia",{
 
                                "$cond":[
 
                                    {"$and":[
 
                                        {"$eq":["$continent","Caribbean"]},
 
                                        {"$eq":[{"$substr":["$name",0,1]}, "B"]}
 
                                    ]},"North America",{
 
                                        "$cond":[
 
                                            {"$and":[
 
                                                {"$eq":["$continent","Caribbean"]},
 
                                                {"$ne":[{"$substr":["$name",0,1]}, "B"]}
 
                                            ]},"South America","$continent"
 
                                        ]
 
                                    }
 
                                ]
 
                            }
 
                        ]
 
                  }
 
                ]
 
            }
 
        }}
 
    ])
 
</div>
 
 
</div>
 
</div>

Revision as of 11:58, 15 June 2018

Country Profile

For these questions you should use aggregate([]) on the collection world

You may find these AGGREGATE examples useful.

Per Capita GDP

Give the name and the per capita GDP for those countries with a population of at least 200 million.

per capita GDP is the GDP divided by the population.

db.world.aggregate([
    {$match:{
        population:{$gte:250000000}
    }},
    {$project:{
        _id:0,
        name:1,
        "per capita GDP": {$divide: ['$gdp',1000000]}
    }}
])
db.world.aggregate([{"$match":{"population":{"$gte":200000000}}},{"$project":{"_id":0,"name":1,"per capita GDP": {"$divide": ["$gdp","$population"]}}}])

Population Density in South America

Give the name and the population density of all countries in South America.

population density is the population divided by the area

Use a $match. {"area":{"$ne":0}}

db.world.aggregate([
    {$match:{continent:'Asia'}},
    {$project:{
        _id:0,
        name:1,
        density: {$divide: ["$population","$area"]}
    }},
])
db.world.aggregate([{$match:{continent:'South America'}},{$project:{_id:0,name:1,density:{$divide:["$population","$area"]}}}])

Population Density for "V"

Give the name and the population density of all countries with name after V in the alphabet.

Note that because Vatican City (with area 0) is in Europe you will get a divide by zero error unless you filter first.

Use a $match.

{ 
  $match: {
    area: {
      "$ne": 0
    }
  }
}
db.world.aggregate([
    {$match: {name: {$gt: 'V'}}},
    {$project: {
        _id: 0,
        name: 1,
        area: 1
    }}
])
db.world.aggregate([{$match:{name:{$gt:'V'}}},{$match:{area:{"$ne":0}}},{$project:{_id:0,name:1,density:{$divide:["$population","$area"]}}}])

Population in millions

Show the name and population in millions for the countries of the continent South America. Divide the population by 1000000 to get population in millions.

db.world.aggregate([
    {$match:{
         
    }},
    {$project:{
        _id: 0,
        name: 1
    }}
])
db.world.aggregate([{"$match":{"continent":{"$eq":"South America"}}},{"$project":{"_id":0,"name":1,"population":{"$divide":["$population",1000000]}}}])

Population density

Show the name and population density for France, Germany, and Italy

db.world.aggregate([
    {$match:{
        name: {$in: ['United Kingdom', 'United States', 'Brazil']},
        population: {$ne: null},
        area: {$ne: 0}
    }},
    {$project:{
        _id: 0,
        name: 1
    }}
])
db.world.aggregate([{"$match":{"name":{"$in":['France','Germany','Italy']},"population":{"$ne":null},"area":{"$ne":0}}},{"$project":{"_id":0,"name":1,"population density":{"$divide":["$population","$area"]}}}])

Continents by area

Order the continents by area from most to least.

db.world.aggregate([
    {$group: {
        _id: "$name",
        area: {$max: "$area"}
    }},
    {$sort: {
        area: -1
    }},
    {$project: {
        _id: 1,
        area: 1
    }}
])
db.world.aggregate([{"$group":{"_id":"$continent","area":{"$sum":"$area"}}},{"$sort":{"area":-1}},{"$project":{"_id":1,"area":1}}])

Big Continents

Show the only two continents with total area greater than 25000000 and then sort from largest to smallest.

db.world.aggregate([
  {$match: {
      continent: "North America"
  }},
  {$project: {
      _id: 0,
      name: 1
  }}
])
db.world.aggregate([{$group:{_id:"$continent",area:{$sum:"$area"}}},{$sort:{area:-1}},{$match:{area:{$gt:25000000}}}])

First and last country by continent

For each continent show the first and last country alphabetically like this:

{ "_id" : "Africa", "from" : "Algeria", "to" : "Zimbabwe" }
{ "_id" : "Asia", "from" : "Afghanistan", "to" : "Yemen" }
{ "_id" : "Caribbean", "from" : "Antigua and Barbuda", "to" : "Trinidad and Tobago" }
{ "_id" : "Eurasia", "from" : "Armenia", "to" : "Russia" }
{ "_id" : "Europe", "from" : "Albania", "to" : "Vatican City" }
{ "_id" : "North America", "from" : "Belize", "to" : "United States" }
{ "_id" : "Oceania", "from" : "Australia", "to" : "Vanuatu" }
{ "_id" : "South America", "from" : "Argentina", "to" : "Venezuela" }
db.world.aggregate([
  {$group:{
      _id:"$continent"
  }},
  {$sort:{
      _id:1
  }}
])
db.world.aggregate([{$sort:{name:1}},{$group:{_id:'$continent',from:{$first:'$name'},to:{$last:'$name'}},},{$sort:{_id:1}}])

Countries beginning with...

Group countries according to the first letter of the name. As shown. Only give "U" through to "Z".

You will need to use the $substr function and the $push aggregate function.

 { "_id" : "U", "list" : [ "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "Uruguay", "Uzbekistan" ] }
 { "_id" : "V", "list" : [ "Vanuatu", "Vatican City", "Venezuela", "Vietnam" ] }
 { "_id" : "Y", "list" : [ "Yemen" ] }
 { "_id" : "Z", "list" : [ "Zambia", "Zimbabwe" ] }
db.world.aggregate([
  {$project: {
    _id: '$name',
    startsWith: {$substr: ['$name', 0, 1]}
  }}, 
  {$match: {
    _id: {$gte: 'U'}
  }},
  {$sort: {_id: 1}}
])
db.world.aggregate([{$group:{_id:{$substr:['$name',0,1]},list:{$push:'$name'}}},{$match:{_id:{$gte:'U'}}},{$sort:{_id:1}}])

Harder Questions

Messing with continent names

Combine North America and South America to America, and then list the continents by area. Biggest first.

db.world.aggregate([
  {$group: {
    _id: {
      $cond: [
        {$eq: ["$continent", "North America"]}, 
        "America",
        {$cond: [
          {$eq: ["$continent", "Asia"]},
          "The East",
          "$continent"
        ]}
      ]
    },
    area: {$sum: "$area"}
  }}, 
  {$sort: {area: -1}},
  {$project: {
    _id: 1,
    area: 1
  }}
])
db.world.aggregate([{"$group":{"_id":{"$cond":[{"$eq":["$continent","South America"]},"America",{"$cond":[{"$eq":["$continent","North America"]},"America","$continent"]}]},"area":{"$sum":"$area"}}},{"$sort":{"area":-1}},{"$project":{"_id":1,"area":1}}]) 

Messing with continent names 2

Show the name and the continent for countries beginning with N - but replace the continent Oceania with Australasia.

db.world.aggregate([
    {$match: {
        name: {$regex: "^N"}
    }},
    {$project: {
        _id: 0,
        name: 1
    }}
])
db.world.aggregate([{"$match":{"name":{"$regex":"^N"}}},{"$project":{"_id":0,"name":1,"continent":{"$cond":[{"$eq":["$continent","Oceania"]},"Australasia","$continent"]}}}])

Messing with continent names 3

Show the name and the continent but:

  • substitute Eurasia for Europe and Asia.
  • substitute America - for each country in North America or South America or Caribbean.

    Only show countries beginning with A or B

If you're struggling you may want to experiment with $and,$or, etc.


db.world.aggregate([{$match:{name:{$regex:"^A|^B"}}},{$project:{_id:0,name:1,continent:{$cond:[{$or:[{$eq:["$continent","Europe"]},{$eq:["$continent","Asia"]}]},"Eurasia",{$cond:[{$or:[{$eq:["$continent","North America"]},{$eq:["$continent","South America"]},{$eq:["$continent","Caribbean"]}]},"America","$continent"]}]}}}])

Messing with continent names 4

Put the continents right...

  • Oceania becomes Australasia
  • Countries in Eurasia and Turkey go to Europe/Asia
  • Caribbean islands starting with 'B' go to North America, other Caribbean islands go to South America



Show the name, the original continent and the new continent of all countries.


db.world.aggregate([{"$project":{"_id":0,"name":1,"original":"$continent","new":{"$cond":[{"$or":[{"$eq":["$continent","Eurasia"]},{"$eq":["$name","Turkey"]}]},"Europe/Asia",{"$cond":[{"$eq":["$continent","Oceania"]},"Australasia",{"$cond":[{"$and":[{"$eq":["$continent","Caribbean"]},{"$eq":[{"$substr":["$name",0,1]},"B"]}]},"North America",{"$cond":[{"$and":[{"$eq":["$continent","Caribbean"]},{"$ne":[{"$substr":["$name",0,1]},"B"]}]},"South America","$continent"]}]}]}]}}}])