Difference between revisions of "AGGREGATE world"
Line 18: | Line 18: | ||
<div class='extra_space' style='width:1em; height:6em;'></div> | <div class='extra_space' style='width:1em; height:6em;'></div> | ||
<div class=q data-lang="py3"> | <div class=q data-lang="py3"> | ||
− | |||
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. | ||
Line 43: | Line 41: | ||
<div class=q data-lang="py3"> | <div class=q data-lang="py3"> | ||
− | Give the <code>name</code> and the < | + | Give the <code>name</code> and the <code>population density</code> of all countries. Ignore results where the density is "None". |
<div class="hint" title="How to calculate population density"> | <div class="hint" title="How to calculate population density"> | ||
population density is the population divided by the area | population density is the population divided by the area | ||
Line 70: | Line 68: | ||
<div class=q data-lang="py3"> | <div class=q data-lang="py3"> | ||
− | + | 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> | ||
− | + | pp.pprint(list( | |
+ | db.world.aggregate([ | ||
+ | {"$match":{ | ||
+ | |||
+ | }}, | ||
+ | {"$project":{ | ||
+ | "_id":0 | ||
+ | }} | ||
+ | ]) | ||
+ | )) | ||
</pre> | </pre> | ||
<div class=ans> | <div class=ans> | ||
− | + | pp.pprint(list( | |
+ | db.world.aggregate([ | ||
+ | {"$match":{ | ||
+ | "continent": {"$eq":"South America"} | ||
+ | }}, | ||
+ | {"$project":{ | ||
+ | "name": 1, | ||
+ | "population": {"$divide": ["$population",1000000]} | ||
+ | }} | ||
+ | ]) | ||
+ | )) | ||
</div> | </div> | ||
</div> | </div> |
Revision as of 12:35, 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]} }} ]) ))
Give the name
and the population density
of all countries. Ignore results where the density is "None".
population density is the population divided by the area
Use a
$match
. {"area":{"$ne":0}}
pp.pprint(list(
db.world.aggregate([
{"$project":{
"_id":0,
"name":1,
"density": {"$divide": [10000,"$area"]}
}},
{"$match":{
"density": {"$ne":None}
}}
])
))
pp.pprint(list(db.world.aggregate([{"$match":{"area":{"$ne":0}}},{"$project":{"_id":0,"name":1,"density":{"$divide":["$population","$area"]}}},{"$match":{"density":{"$ne":None}}}])))
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.
pp.pprint(list(
db.world.aggregate([
{"$match":{
}},
{"$project":{
"_id":0
}}
])
))
pp.pprint(list(
db.world.aggregate([
{"$match":{
"continent": {"$eq":"South America"}
}},
{"$project":{
"name": 1,
"population": {"$divide": ["$population",1000000]}
}}
])
))