From 9fd53c8c06527a96c885c290abd9c430658e7d84 Mon Sep 17 00:00:00 2001 From: porcupine Date: Thu, 16 Oct 2025 13:49:40 -0700 Subject: [PATCH 1/2] base mongodb tim aggregation script --- mongotim.py | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 mongotim.py diff --git a/mongotim.py b/mongotim.py new file mode 100644 index 0000000..f54c269 --- /dev/null +++ b/mongotim.py @@ -0,0 +1,58 @@ +import pymongo +import json + +# setup mongo client and db +client = pymongo.MongoClient() +db = client['homework'] + +# load json data +data = json.loads(open('example_tim_data.json').read()) + +tim_col = db.tim +# load data into mongo if we don't already have it +if not tim_col.estimated_document_count(): + tim_col.insert_many(data) +print('tim has %d records' % tim_col.estimated_document_count()) +# ensure team col is empty +team_col = db.team_col +team_col.drop() + + +# Define mongodb aggregation pipeline +pipeline = [ + { + "$group": { + "_id": "$team_num", + "average_balls_scored": { "$avg": "$num_balls" }, + "least_balls_scored": { "$min": "$num_balls" }, + "most_balls_scored": { "$max": "$num_balls" }, + "num_matches_played": { "$sum": 1 }, + "num_climbed_matches": { + "$sum": { + "$cond": [ { "$eq": ["$climbed", True] }, 1, 0 ] + } + } + } + }, + { + "$project": { + "_id": 1, + "average_balls_scored": 1, + "least_balls_scored": 1, + "most_balls_scored": 1, + "num_matches_played": 1, + "climbed_fraction": { + "$divide": ["$num_climbed_matches", "$num_matches_played"] + } + } + }, + { + # Add the $out stage as the final step, writing to "team_col" as string + "$out": 'team_col' + } +] + +tim_col.aggregate(pipeline) +team_col = db.team_col +for td in team_col.find(): + print(td.pop('_id'), td) From 9cbc97bb1d9a2ac9cc4b2f151f5f3d158ac90199 Mon Sep 17 00:00:00 2001 From: porcupine Date: Thu, 16 Oct 2025 13:52:07 -0700 Subject: [PATCH 2/2] rename with user prefix --- mongotim.py => jlivni_mongo_tim.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename mongotim.py => jlivni_mongo_tim.py (100%) diff --git a/mongotim.py b/jlivni_mongo_tim.py similarity index 100% rename from mongotim.py rename to jlivni_mongo_tim.py