Difference between revisions of "FIND from WORLD"
Line 222: | Line 222: | ||
<div class=ans> | <div class=ans> | ||
pp.pprint(list(db.world.aggregate([{"$match":{"name":{"$regex":"^N"}}},{"$project":{"name":1,"continent":{"$cond":[{"$eq":["$continent","Oceania"]},"Austalasia","$continent"]},"_id":0}}]))) | pp.pprint(list(db.world.aggregate([{"$match":{"name":{"$regex":"^N"}}},{"$project":{"name":1,"continent":{"$cond":[{"$eq":["$continent","Oceania"]},"Austalasia","$continent"]},"_id":0}}]))) | ||
+ | </div> | ||
+ | </div> | ||
+ | |||
+ | <div class=q data-lang="py3"> | ||
+ | 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> | ||
+ | pp.pprint(list( | ||
+ | db.world.aggregate([ | ||
+ | {"$match":{ | ||
+ | "name":{"$regex":"^N"} | ||
+ | }}, | ||
+ | {"$project":{ | ||
+ | "_id":0, | ||
+ | "name":1 | ||
+ | }} | ||
+ | ]) | ||
+ | )) | ||
+ | </pre> | ||
+ | <div class=ans> | ||
+ | pp.pprint(list(db.world.aggregate([{"$match":{"name":{"$regex":"^N"}}},{"$project":{"_id":0,"name":1,"continent":{"$cond":[{"$eq":["$continent","Oceania"]},"Australasia","$continent"]}}}]))) | ||
+ | </div> | ||
+ | </div> | ||
+ | |||
+ | <div class=q data-lang="py3"> | ||
+ | Show the <b>name</b> and the <b>continent</b> but:<br/><br/> | ||
+ | substitute <b>Eurasia</b> for <b>Europe</b> and <b>Asia</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. | ||
+ | <pre class=def></pre> | ||
+ | <div class=ans> | ||
+ | pp.pprint(list( | ||
+ | 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"]} | ||
+ | ]} | ||
+ | }} | ||
+ | ]) | ||
+ | )) | ||
+ | </div> | ||
+ | </div> | ||
+ | |||
+ | <div class=q data-lang="py3"> | ||
+ | Put the continents right... | ||
+ | <br/><br/> | ||
+ | <b>Oceania</b> becomes <b>Australasia</b><br/> | ||
+ | Countries in <b>Eurasia</b> and <b>Turkey</b> go to <b>Europe/Asia</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/> | ||
+ | <b>Show the name, the original continent and the new continent of all countries.</b> | ||
+ | <pre class=def></pre> | ||
+ | <div class=ans> | ||
+ | pp.pprint(list( | ||
+ | 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" | ||
+ | ] | ||
+ | } | ||
+ | ] | ||
+ | } | ||
+ | ] | ||
+ | } | ||
+ | ] | ||
+ | } | ||
+ | }} | ||
+ | ]) | ||
+ | )) | ||
</div> | </div> | ||
</div> | </div> |
Revision as of 13:40, 29 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)
Working with the world
collection of countries
This tutorial introduces NoSQL using MonogDB and PyMongo under Python3.4. We will be using the find()
command and comparison functions on the collection world which contains details of around 250 countries of the world:
FIND examples are available.
Observe the result of running a simple mongoDB command.
FIND name, continent, population FROM world
pp.pprint(list( db.world.find({},{ "name":1, "continent":1, "population":1, "_id":0 }) ))
pp.pprint(list(db.world.find({},{"name":1,"continent":1,"population":1,"_id":0})))
Show the name for the countries that have a population of at least 200 million. 200 million is 200000000, there are eight zeros.
pp.pprint(list( db.world.find( {"population":{"$gt":200000000}}, {"name":1, "_id":0} ) ))
pp.pprint(list(db.world.find({"population":{"$gt":200000000}},{"name":1, "_id":0})))
Give the name and the per capita GDP for those countries with a population of at least 200 million.
AGGREGATE examples are available.
pp.pprint(list( db.world.aggregate([ {"$match":{"population":{"$gt":200000000}}}, {"$project":{"name":1, "gdp/population":{"$divide":["$gdp","$population"]}, "_id":0}} ]) ))
pp.pprint(list(db.world.aggregate([{"$match":{"population":{"$gt":200000000}}},{"$project":{"name":1, "gdp/population":{"$divide":["$gdp","$population"]}, "_id":0}}])))
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":{"continent":{"$eq":"South America"}}}, {"$project":{"name":1, "population in millions":{"$divide":["$population",1000000]}, "_id":0}} ]) ))
pp.pprint(list(db.world.aggregate([{"$match":{"continent":{"$eq":"South America"}}},{"$project":{"name":1, "population in millions":{"$divide":["$population",1000000]}, "_id":0}}])))
Show the name and population for France, Germany, Italy
pp.pprint(list( db.world.aggregate([ {"$match":{"name":{"$in":["France", "Germany", "Italy"]}}}, {"$project":{"name":1, "population":1, "_id":0}} ]) ))
pp.pprint(list(db.world.aggregate([{"$match":{"name":{"$in":["France", "Germany", "Italy"]}}},{"$project":{"name":1, "population":1, "_id":0}}])))
Show the countries which have a name that includes the word 'United'
pp.pprint(list( db.world.find( {"name":{"$regex":"\s?United\s?"}}, {"name":1, "_id":0} ) ))
pp.pprint(list(db.world.find({"name":{"$regex":"\s?United\s?"}},{"name":1, "_id":0})))
Two ways to be big: A country is big if it has an area of more than 3 million sq km or it has a population of more than 250 million.
Show the countries that are big by area or big by population. Show name, population and area
pp.pprint(list( db.world.find( {"$or":[{"area":{"$gt":3000000}},{"population":{"$gt":250000000}}]}, {"name":1,"population":1,"area":1,"_id":0} ) ))
pp.pprint(list(db.world.find({"$or":[{"area":{"$gt":3000000}},{"population":{"$gt":250000000}}]},{"name":1,"population":1,"area":1,"_id":0})))
USA and China are big in population and big by area. Exclude these countries.
Show the countries that are big by area or big by population but not both. Show name, population and area.
pp.pprint(list( db.world.find( {"$or":[ {"area":{"$gt":3000000},"population":{"$lt":250000000}}, {"area":{"$lt":3000000},"population":{"$gt":250000000}} ]}, {"name":1,"population":1,"area":1,"_id":0} ) ))
pp.pprint(list(db.world.find({"$or":[{"area":{"$gt":3000000},"population":{"$lt":250000000}},{"area":{"$lt":3000000},"population":{"$gt":250000000}}]}, {"name":1,"population":1,"area":1,"_id":0})))
Show the name and population in millions and the GDP in billions for the countries of the continent 'South America'. Use the ROUND function to show the values to two decimal places.
Introducing MapReduce
For South America show population in millions and GDP in billions to 2 decimal places.
from bson.code import Code temp = db.world.map_reduce( query={"continent":"South America"}, map=Code("function(){emit(this.name,{'gdp':this.gdp,'pop':this.population});}"), reduce=Code("""function(key, values){ return values} """), finalize=Code("""function(k, v){ return { 'pop in millions':Math.round(v['pop']*1000000)/1000000, 'gdp in billions':Math.round(v['gdp']*1000000000)/1000000000 }; } """), out={"inline":1} ) pp.pprint(temp['results']);
from bson.code import Code temp = db.world.map_reduce(query={"continent":"South America"},map=Code("function(){emit(this.name, {'gdp':this.gdp,'pop':this.population});}"),reduce=Code("""function(key, values){ return values} """),finalize=Code("""function(k, v){return {'pop in millions':Math.round(v['pop']*1000000)/1000000,'gdp in billions':Math.round(v['gdp']*1000000000)/1000000000};}"""),out={"inline":1}) pp.pprint(temp['results']);
Show the per-capita GDP for those countries with a GDP of at least one trillion (1000000000000; that is 12 zeros). Round this value to the nearest 1000.
Show per-capita GDP for the trillion dollar countries to the nearest $1000.
from bson.code import Code temp = db.world.map_reduce( map=Code("function(){if(this.gdp > 1000000000000)emit(this.name,{'gdp':this.gdp,'pop':this.population});}"), reduce=Code("""function(key, values){ return values} """), finalize=Code("""function(k, v){ return {'per-capita GDP':Math.round((v['gdp']/v['pop'])/1000)*1000}; } """), out={"inline":1} ) pp.pprint(temp['results']);
from bson.code import Code temp = db.world.map_reduce(map=Code("function(){if(this.gdp > 1000000000000)emit(this.name,{'gdp':this.gdp,'pop':this.population});}"),reduce=Code("""function(key, values){ return values} """),finalize=Code("""function(k, v){ return {'per-capita GDP':Math.round((v['gdp']/v['pop'])/1000)*1000};}"""),out={"inline":1}) pp.pprint(temp['results']);
Harder Questions
Find out more about $cond
at AGGREGATE_examples
Show the name and the continent - but substitute Australasia for Oceania - for countries beginning with N.
pp.pprint(list( db.world.aggregate([ {"$match":{"name":{"$regex":"^N"}}}, {"$project":{ "name":1, "continent":{"$cond":[{"$eq":["$continent","Oceania"]},"Austalasia","$continent"]}, "_id":0 }} ]) ))
pp.pprint(list(db.world.aggregate([{"$match":{"name":{"$regex":"^N"}}},{"$project":{"name":1,"continent":{"$cond":[{"$eq":["$continent","Oceania"]},"Austalasia","$continent"]},"_id":0}}])))
Show the name and the continent for countries beginning with N - but replace the continent Oceania with Australasia.
pp.pprint(list( db.world.aggregate([ {"$match":{ "name":{"$regex":"^N"} }}, {"$project":{ "_id":0, "name":1 }} ]) ))
pp.pprint(list(db.world.aggregate([{"$match":{"name":{"$regex":"^N"}}},{"$project":{"_id":0,"name":1,"continent":{"$cond":[{"$eq":["$continent","Oceania"]},"Australasia","$continent"]}}}])))
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.
pp.pprint(list(
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"]} ]} }} ])
))
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.
pp.pprint(list(
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" ] } ] } ] } ] } }} ])
))