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

Difference between revisions of "AGGREGATE world"

From NoSQLZoo
Jump to: navigation, search
Line 20: Line 20:
 
Give the <code>name</code> and the <b>per capita GDP</b> for those countries with a <code>population</code> of at least 200 million.
 
Give the <code>name</code> and the <b>per capita GDP</b> 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 GDP/population
+
per capita GDP is the GDP divided by the population.
 
</div>
 
</div>
 
<pre class=def>
 
<pre class=def>
Line 32: Line 32:
 
             "name":1,
 
             "name":1,
 
             "per capita GDP": {"$divide": ["$gdp",1000000]}
 
             "per capita GDP": {"$divide": ["$gdp",1000000]}
 +
        }}
 +
    ])
 +
))
 +
</pre>
 +
<div class=ans>pp.pprint(list(db.world.aggregate([{"$match":{"population":{"$gte":200000000}}},{"$project":{"_id":0,"name":1,"per capita GDP": {"$divide": ["$gdp","$population"]}}}])))
 +
</div>
 +
</div>
 +
 +
<div class=q data-lang="py3">
 +
Give the <code>name</code> and the <b>population density</b> of all countries.
 +
<div class="hint" title="How to calculate population density">
 +
population density is the population divided by the area
 +
</div>
 +
<pre class=def>
 +
pp.pprint(list(
 +
    db.world.aggregate([
 +
        {"$project":{
 +
            "_id":0,
 +
            "name":1,
 +
            "per capita GDP": {"$divide": [10000,"$area"]}
 
         }}
 
         }}
 
     ])
 
     ])

Revision as of 13:14, 17 July 2015

#ENCODING
import io
import sys
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-16')
#MONGO
from pymongo import MongoClient
client = MongoClient()
client.progzoo.authenticate('scott','tiger')
db = client['progzoo']
#PRETTY
import pprint
pp = pprint.PrettyPrinter(indent=4)

Country Profile

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

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.

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

Give the name and the population density of all countries.

population density is the population divided by the area

pp.pprint(list(
    db.world.aggregate([
        {"$project":{
            "_id":0,
            "name":1,
            "per capita GDP": {"$divide": [10000,"$area"]}
        }}
    ])
))
pp.pprint(list(db.world.aggregate([{"$match":{"population":{"$gte":200000000}}},{"$project":{"_id":0,"name":1,"per capita GDP": {"$divide": ["$gdp","$population"]}}}])))