Last active
January 15, 2016 14:42
-
-
Save ferreiro/3b9d75aac5f6dc756e5b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# MapReduce: gasto total realizado por las mujeres que han realizdo EXACTAMENTE | |
# 3 compras. | |
@get('/spending_female_3_orders_mr') | |
def spending_female_3_orders_mr(): | |
mapper = Code(""" | |
function countryMap() | |
{ | |
if (this.gender == "Female") { | |
var total = 0; | |
var orders = this.orders; | |
if (orders) { | |
if (orders.length == 3) { | |
for (i=0; i < orders.length; i++) { | |
total += orders[i].total | |
} | |
} | |
} | |
emit(this.gender, { count: total }); | |
} | |
} | |
""") | |
reducer = Code(""" | |
function countryReduce(key, values) { | |
var total = 0; | |
for (var i = 0; i < values.length; i++) { | |
total += values[i].count; | |
} | |
return { count: total } | |
} | |
""") | |
results = db.users.inline_map_reduce(mapper, reducer) | |
return template('table_map_reduce', results=results, count=len(results)); | |
# Aggregation Pipeline: gasto total realizado por las mujeres que han realizdo | |
# EXACTAMENTE 3 compras. | |
@get('/spending_female_3_orders_agg') | |
def spending_female_3_orders_agg(): | |
pipeline = [ | |
{ | |
"$match" : { | |
"gender" : "Female", | |
"orders": { "$size" : 3 } | |
} | |
}, | |
{ "$unwind": "$orders" }, | |
{ "$group": {"_id": "$gender", "count": {"$sum": "$orders.total" }}}, | |
{ "$sort": SON([("count", 1), ("_id", -1)])} | |
] | |
results= list(db.users.aggregate(pipeline)) | |
return template('table_pipeline', results=results, count=len(results)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment