You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

120 lines
3.3 KiB

  1. from pymongo import MongoClient
  2. import datetime
  3. client = MongoClient("mongodb://localhost")
  4. db = client['test']
  5. def ex1():
  6. cursor = db.restaurants.find({'cuisine': 'Irish'})
  7. print(len(list(cursor)))
  8. cursor = db.restaurants.find({'cuisine': 'Russian'})
  9. print(len(list(cursor)))
  10. cursor = db.restaurants.find({
  11. '$or': [{'cuisine': 'Irish'}, {'cuisine': 'Russian'}]})
  12. cursor_list = list(cursor)
  13. print(len(cursor_list))
  14. cursor = db.restaurants.find({
  15. 'address.building': '284',
  16. 'address.street': 'Prospect Park West',
  17. 'address.zipcode': '11215'})
  18. print(list(cursor))
  19. def atomic_insert_bestrest():
  20. bestrest = {
  21. 'address': {
  22. 'building': '126',
  23. 'coord': [-73.9557413, 40.7720266],
  24. 'street': 'Sportivnaya',
  25. 'zipcode': '420500' },
  26. 'borough': 'Innopolis',
  27. 'cuisine': 'Serbian',
  28. 'name': 'The Best Restaurant',
  29. 'restaurant_id': '41712354',
  30. 'grades': [{
  31. 'date': datetime.datetime(2023, 4, 4, 0, 0),
  32. 'grade': 'A',
  33. 'score': 11
  34. }]
  35. }
  36. db.restaurants.delete_many({'restaurant_id': '41712354'})
  37. db.restaurants.insert_one(bestrest)
  38. cursor = db.restaurants.find({'restaurant_id': '41712354'})
  39. print(list(cursor))
  40. def delete_brooklyn1_ThaiN():
  41. db.restaurants.delete_one({'borough': 'Brooklyn'})
  42. db.restaurants.delete_many({'cuisine': 'Thai'})
  43. def insert_random_item_for_ex4():
  44. bestrest = {
  45. 'address': {
  46. 'building': '126',
  47. 'coord': [-73.9557413, 40.7720266],
  48. 'street': 'Prospect Park West',
  49. 'zipcode': '177013' },
  50. 'borough': 'Innopolis',
  51. 'cuisine': 'Serbian',
  52. 'name': 'The Best Restaurant',
  53. 'restaurant_id': '177013123',
  54. 'grades': [{
  55. 'date': datetime.datetime(2023, 4, 4, 0, 0),
  56. 'grade': 'B',
  57. 'score': 11
  58. }]
  59. }
  60. db.restaurants.delete_many({'restaurant_id': '177013123'})
  61. db.restaurants.insert_one(bestrest)
  62. def ex4():
  63. cursor = db.restaurants.find({
  64. 'address.street': 'Prospect Park West'
  65. })
  66. restaurants = list(cursor)
  67. for restaurant in restaurants:
  68. cnt = 0
  69. for grade in restaurant['grades']:
  70. if grade['grade'] == 'A':
  71. cnt = cnt + 1
  72. if cnt >= 1:
  73. result = db.restaurants.delete_one({'restaurant_id': restaurant['restaurant_id']})
  74. print(f'Deleted {result.deleted_count} items')
  75. else:
  76. result = db.restaurants.update_one(
  77. {'restaurant_id': restaurant['restaurant_id']},
  78. {'$push': {
  79. 'grades': {
  80. 'date': datetime.datetime.utcnow(),
  81. 'grade': 'A',
  82. 'score': 11
  83. }
  84. }})
  85. print('Modified:')
  86. print(list(db.restaurants.find({'restaurant_id': restaurant['restaurant_id']})))
  87. def main():
  88. ex1()
  89. atomic_insert_bestrest()
  90. delete_brooklyn1_ThaiN()
  91. insert_random_item_for_ex4()
  92. ex4()
  93. if __name__ == "__main__":
  94. main()