Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- Created on Sun Oct 12 17:25:10 2014
- @author: vikramk3
- """
- #!/usr/bin/env python
- """
- Use an aggregation query to answer the following question.
- Provide a list of unique users.
- Please modify only the 'make_pipeline' function so that it creates and returns an aggregation
- pipeline that can be passed to the MongoDB aggregate function. As in our examples in this lesson,
- the aggregation pipeline should be a list of one or more dictionary objects.
- Please review the lesson examples if you are unsure of the syntax.
- This code was run locally on my machine.
- """
- def get_db(db_name):
- from pymongo import MongoClient
- client = MongoClient('localhost:27017')
- db = client[db_name]
- return db
- def make_pipeline():
- # complete the aggregation pipeline
- pipeline = [{"$match":{"address.state":"TX"}},{"$group":{"_id":"$created.uid","count":{"$sum":1}}},{"$sort":{"count":-1}}]
- return pipeline
- def aggregate(db, pipeline):
- result = db.cities.aggregate(pipeline)
- return result
- if __name__ == '__main__':
- db = get_db('examples')
- pipeline = make_pipeline()
- result = aggregate(db, pipeline)
- #print result
- import pprint
- pprint.pprint(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement