Last active
April 14, 2017 20:39
-
-
Save RWaltersMA/f610bd74cba4f61ad81730c41ac19763 to your computer and use it in GitHub Desktop.
Find in Python using Aggregation Framework
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
from pymongo import MongoClient | |
# Connect to the MongoDB, change the connection string per your MongoDB environment | |
client = MongoClient(port=27017) | |
# Set the db object to point to the business database | |
db=client.business | |
# Showcasing the count() method of find, count the total number of 5 ratings | |
print('The number of 5 star reviews:') | |
fivestarcount = db.reviews.find({'rating': 5}).count() | |
print(fivestarcount) | |
# Not let's use the aggregation framework to sum the occurrence of each rating across the entire data set | |
print('\nThe sum of each rating occurance across all data grouped by rating ') | |
stargroup=db.reviews.aggregate( | |
# The Aggregation Pipeline is defined as an array of different operations | |
[ | |
# The first stage in this pipe is to group data | |
{ '$group': | |
{ '_id': "$rating", | |
"count" : | |
{ '$sum' :1 } | |
} | |
}, | |
# The second stage in this pipe is to sort the data | |
{"$sort": { "_id":1} | |
} | |
# Close the array with the ] tag | |
] ) | |
# Print the result | |
for group in stargroup: | |
print(group) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment