AGGREGATE world
From NoSQLZoo
#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 GDP/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"]}}}])))