Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pymongo
- client = pymongo.MongoClient()
- db = client["bdm"]
- def find_distinct_cities(db, collection_name):
- cities = db[collection_name].find().distinct("city")
- cities.sort()
- return cities
- def count_reviews_since(db, collection, since):
- return db[collection].count_documents({
- "date": {"$gte": since}
- })
- def find_closed(db, collection_name):
- return db[collection_name].find(
- {"open": False},
- {"_id": 0, "name": 1, "full_address": 1, "stars": 1}
- )
- def find_no_positive_reviews(db, collection_name):
- return db[collection_name].find({
- "$and": [{"votes.funny": 0}, {"votes.useful": 0}]
- }).sort("name")
- def remove_by_rating(db, collection_name, rating=2):
- db[collection_name].delete_many({"stars": rating})
- def find_number_of_tips(db, collection_name):
- pipeline = [
- {"$group": {"_id": "$business_id",
- "count": {"$sum": 1}}},
- {"$sort": {"count": 1}}
- ]
- return db[collection_name].aggregate(pipeline)
- def find_average_rating(db, collection_name):
- pipeline = [
- {"$group": {"_id": "$business_id",
- "average": {"$avg": "$stars"}}},
- {"$match": {"average": {"$gte": 4}}}
- ]
- return db[collection_name].aggregate(pipeline)
- if __name__ == '__main__':
- for i in find_average_rating(db, "review"):
- print(i)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement