Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.41 KB | None | 0 0
  1. import pymongo
  2.  
  3. client = pymongo.MongoClient()
  4. db = client["bdm"]
  5.  
  6.  
  7. def find_distinct_cities(db, collection_name):
  8.     cities = db[collection_name].find().distinct("city")
  9.     cities.sort()
  10.     return cities
  11.  
  12.  
  13. def count_reviews_since(db, collection, since):
  14.     return db[collection].count_documents({
  15.         "date": {"$gte": since}
  16.     })
  17.  
  18.  
  19. def find_closed(db, collection_name):
  20.     return db[collection_name].find(
  21.         {"open": False},
  22.         {"_id": 0, "name": 1, "full_address": 1, "stars": 1}
  23.     )
  24.  
  25.  
  26. def find_no_positive_reviews(db, collection_name):
  27.     return db[collection_name].find({
  28.         "$and": [{"votes.funny": 0}, {"votes.useful": 0}]
  29.     }).sort("name")
  30.  
  31.  
  32. def remove_by_rating(db, collection_name, rating=2):
  33.     db[collection_name].delete_many({"stars": rating})
  34.  
  35.  
  36. def find_number_of_tips(db, collection_name):
  37.     pipeline = [
  38.         {"$group": {"_id": "$business_id",
  39.                     "count": {"$sum": 1}}},
  40.         {"$sort": {"count": 1}}
  41.     ]
  42.     return db[collection_name].aggregate(pipeline)
  43.  
  44.  
  45. def find_average_rating(db, collection_name):
  46.     pipeline = [
  47.         {"$group": {"_id": "$business_id",
  48.                     "average": {"$avg": "$stars"}}},
  49.         {"$match": {"average": {"$gte": 4}}}
  50.     ]
  51.     return db[collection_name].aggregate(pipeline)
  52.  
  53.  
  54. if __name__ == '__main__':
  55.     for i in find_average_rating(db, "review"):
  56.         print(i)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement