|
| 1 | +from pymongo import MongoClient |
| 2 | +import time |
| 3 | +from datetime import datetime |
| 4 | + |
| 5 | + |
| 6 | +# Iniciar Mongo ------------------------------------------- |
| 7 | + |
| 8 | +client = MongoClient("mongodb://127.0.0.1:27017") |
| 9 | +db = client["final"] |
| 10 | +collection = db["viajes"] |
| 11 | + |
| 12 | + |
| 13 | +# App ---------------------------------------------------------- |
| 14 | + |
| 15 | +def menu(): |
| 16 | + |
| 17 | + print('Bienvenido, eliga una opcion') |
| 18 | + print('--------------------------------------') |
| 19 | + print('1. ¿Cuántos viajes hizo el usuario Bunny?') |
| 20 | + print('2. ¿Quiénes hicieron un viaje redondo (es decir salieron y llegaron al mismo destino)?') |
| 21 | + print('3. ¿Cuál fue el usuario con más viajes (puede ser más de uno)?') |
| 22 | + print('4. ¿Cuál fue el origen y destino del viaje más largo (en tiempo)?') |
| 23 | + print('5. ¿Qué orígenes y destinos son los más populares?') |
| 24 | + print('6. ¿En qué días de la semana se realizan la mayoría de los viajes?') |
| 25 | + print('--------------------------------------') |
| 26 | + print('8. Insertar Viaje') |
| 27 | + print('9. Modificar Viaje') |
| 28 | + |
| 29 | + opcion = input('opcion: ') |
| 30 | + if opcion == '1' : |
| 31 | + |
| 32 | + rows = collection.count_documents({"usuario.nombre":"Bunny", "usuario.nacimiento": 2003}) |
| 33 | + for row in rows: |
| 34 | + print(row) |
| 35 | + |
| 36 | + time.sleep(5) |
| 37 | + menu() |
| 38 | + |
| 39 | + elif opcion == '2' : |
| 40 | + |
| 41 | + rows = collection.find({ "$expr": { "$eq": ["$lugarOrigen", "$lugarDestino"] }}) |
| 42 | + for row in rows: |
| 43 | + print(row) |
| 44 | + |
| 45 | + time.sleep(5) |
| 46 | + menu() |
| 47 | + |
| 48 | + elif opcion == '3' : |
| 49 | + |
| 50 | + rows = collection.aggregate([ { "$group": { "_id": {"usuario.nombre":"$usuario.nombre", "usuario.nacimiento":"$usuario.nacimiento" }, "viajes": { "$count": "$numero" } } },]) |
| 51 | + for row in rows: |
| 52 | + print(row) |
| 53 | + |
| 54 | + time.sleep(5) |
| 55 | + menu() |
| 56 | + |
| 57 | + elif opcion == '4' : |
| 58 | + |
| 59 | + rows = collection.aggregate( [ { "$group": { "_id": "$numero", "largo": { "$substract": ["fechaDestino","fechaOrigen"] } } }, { "$project": { "numero": 1, "fechaOrigen": 1, "fechaDestino": 1} }, { "$sort": { "largo": -1 } }, { "$limit": 1 } ]) |
| 60 | + for row in rows: |
| 61 | + print(row) |
| 62 | + |
| 63 | + time.sleep(5) |
| 64 | + menu() |
| 65 | + |
| 66 | + elif opcion == '5' : |
| 67 | + |
| 68 | + print('Origen mas popular') |
| 69 | + rows = collection.aggregate([ { "$group": { "_id": "$lugarOrigen", "cant": { "$count": "$lugarOrigen"} } }, { "$project": {"lugarOrigen": 1} }, { "$sort": { "cant": -1 } }, { "$limit": 1 } ]) |
| 70 | + for row in rows: |
| 71 | + print(row) |
| 72 | + |
| 73 | + print('--------------------------------------') |
| 74 | + |
| 75 | + print('Destino mas popular') |
| 76 | + rows = collection.aggregate([ { "$group": { "_id": "$lugarDestino", "cant": { "$count": "$lugarDestino"} } }, { "$project": {"lugarDestino": 1} }, { "$sort": { "cant": -1 } }, { "$limit": 1 } ]) |
| 77 | + for row in rows: |
| 78 | + print(row) |
| 79 | + |
| 80 | + time.sleep(5) |
| 81 | + menu() |
| 82 | + |
| 83 | + elif opcion == '6' : |
| 84 | + |
| 85 | + collection.aggregate([ { "$group": { "dayOfWeek": { "$dayOfWeek": "$fechaOrigen"}, "cant": { "$count": "$numero"} } }, { "$project": {"dayOfWeek": 1} }, { "$sort": { "cant": -1 } }, { "$limit": 3 } ]) |
| 86 | + time.sleep(5) |
| 87 | + menu() |
| 88 | + |
| 89 | + elif opcion == '7' : |
| 90 | + |
| 91 | + numero = input("numero: ") |
| 92 | + usuario_nombre = input("usuario nombre: ") |
| 93 | + usuario_nacimiento = input("usuario nacimiento: ") |
| 94 | + lugarOrigen = input("lugarOrigen: ") |
| 95 | + fechaOrigen = input("fechaOrigen (2020-04-12T09:28): ") |
| 96 | + lugarDestino = input("lugarDestino: ") |
| 97 | + fechaDestino = input("fechaDestino (2020-04-12T09:28): ") |
| 98 | + |
| 99 | + fechaOrigen = datetime.fromisoformat(fechaOrigen) |
| 100 | + fechaDestino = datetime.fromisoformat(fechaDestino) |
| 101 | + |
| 102 | + collection.insert_one({"numero": numero, "usario": { "nombre": usuario_nombre, "nacimiento": usuario_nacimiento }, "lugarOrigen": lugarOrigen, "fechaorigen": fechaOrigen, "lugarDestino": lugarDestino, "fechaDestino": fechaDestino}) |
| 103 | + |
| 104 | + time.sleep(5) |
| 105 | + menu() |
| 106 | + |
| 107 | + elif opcion == '8' : |
| 108 | + |
| 109 | + numero = input("numero de viaje a modificar: ") |
| 110 | + opcion = input("Modificar Lugar Origen(1) o Destino?(2): ") |
| 111 | + |
| 112 | + if opcion == '1': |
| 113 | + |
| 114 | + lugarOrigen = input("nuevo lugarOrigen: ") |
| 115 | + collection.update_one({"numero": numero}, {"$set": {"lugarOrigen": lugarOrigen}}) |
| 116 | + |
| 117 | + elif opcion == '2': |
| 118 | + |
| 119 | + lugarDestino = input("nuevo lugarDestino: ") |
| 120 | + collection.update_one({"numero": numero}, {"$set": {"lugarDestino": lugarDestino}}) |
| 121 | + |
| 122 | + else: |
| 123 | + print("opcion incorrecta") |
| 124 | + time.sleep(2) |
| 125 | + menu() |
| 126 | + |
| 127 | + print("viaje modificado con exito") |
| 128 | + |
| 129 | + time.sleep(5) |
| 130 | + menu() |
| 131 | + |
| 132 | + else: |
| 133 | + print("opcion incorrecta") |
| 134 | + time.sleep(5) |
| 135 | + menu() |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | +# Run program |
| 141 | +menu() |
0 commit comments