├── Add Cars.py ├── README.md ├── Tutorial 1.py ├── Tutorial 2.py └── data.csv /Add Cars.py: -------------------------------------------------------------------------------- 1 | import pymongo 2 | import datetime 3 | import csv 4 | 5 | client = pymongo.MongoClient('mongodb://user:password@ip_address/') 6 | 7 | db = client['dealership'] 8 | 9 | cars = db['cars'] 10 | customers = db['customers'] 11 | purchases = db['purchases'] 12 | 13 | def add_car_data(filename): 14 | with open(filename, 'r') as file: 15 | columns = file.readline().split(',') 16 | file = csv.reader(file) 17 | columns_needed = ['Make', 'Model', 'Year', 'Engine HP', 'Vehicle Size', 'Vehicle Style', 'MSRP'] 18 | indexs = list(filter(lambda x: columns[x].strip() in columns_needed , [i for i in range(len(columns))])) 19 | number_columns = {"MSRP", "Year", "Engine HP"} 20 | 21 | documents = [] 22 | for row in file: 23 | document = {} 24 | for count, index in enumerate(indexs): 25 | data = row[index] 26 | if columns_needed[count] in number_columns: 27 | try: 28 | data = float(data) 29 | except: 30 | continue 31 | document[columns_needed[count]] = data 32 | 33 | documents.append(document) 34 | 35 | cars.insert_many(documents)add_car_data('data.csv') 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MongoDB-Tutorial 2 | 3 | # 💻 Launch Your Software Development Career Today! 4 | 5 | 🎓 **No degree? No problem!** My program equips you with everything you need to break into tech and land an entry-level software development role. 6 | 7 | 🚀 **Why Join?** 8 | - 💼 **$70k+ starting salary potential** 9 | - 🕐 **Self-paced:** Complete on your own time 10 | - 🤑 **Affordable:** Low risk compared to expensive bootcamps or degrees 11 | - 🎯 **45,000+ job openings** in the market 12 | 13 | 👉 **[Start your journey today!](https://techwithtim.net/dev)** 14 | No experience needed—just your determination. Future-proof your career and unlock six-figure potential like many of our students have! 15 | -------------------------------------------------------------------------------- /Tutorial 1.py: -------------------------------------------------------------------------------- 1 | import pymongo 2 | import datetime 3 | 4 | client = pymongo.MongoClient('mongodb://user:password@server_ip/') 5 | 6 | db = client['dealership'] 7 | 8 | cars = db['cars'] 9 | customers = db['customers'] 10 | purchases = db['purchases'] 11 | 12 | def add_car(make, model, year, engine_HP, msrp): 13 | document = { 14 | 'Make': make, 15 | 'Model': model, 16 | 'Year': year, 17 | 'Engine HP': engine_HP, 18 | 'MSRP': msrp, 19 | 'Date Added': datetime.datetime.now() 20 | } 21 | return cars.insert_one(document) 22 | 23 | def add_customer(first_name, last_name, dob): 24 | document = { 25 | 'First Name': first_name, 26 | 'Last Name': last_name, 27 | 'Date of Birth': dob, 28 | 'Date Added': datetime.datetime.now(), 29 | } 30 | return customers.insert_one(document) 31 | 32 | def add_purchase(car_id, customer_id, method): 33 | document = { 34 | 'Car ID': car_id, 35 | 'Customer ID': customer_id, 36 | 'Method': method, 37 | 'Date': datetime.datetime.now() 38 | } 39 | return purchases.insert_one(document) 40 | 41 | car = add_car('Mazda', 'CX5', 2020, 250, 45000) 42 | customer = add_customer('Tim', 'Tech', 'Jul 20, 2000') 43 | purchase = add_purchase(car.inserted_id, customer.inserted_id, 'Cash') 44 | 45 | result = cars.update_many({'Make': 'Ford', 'Model': 'CX5'}, 46 | {'$set': {'Make': 'Ford', 'Model': 'Edge'}}) 47 | -------------------------------------------------------------------------------- /Tutorial 2.py: -------------------------------------------------------------------------------- 1 | import pymongo 2 | import datetime 3 | import csv 4 | import pprint 5 | 6 | printer = pprint.PrettyPrinter() 7 | 8 | client = pymongo.MongoClient('mongodb://tim:tim@172.105.17.246/') 9 | 10 | db = client['dealership'] 11 | 12 | cars = db['cars'] 13 | customers = db['customers'] 14 | purchases = db['purchases'] 15 | 16 | def add_car(make, model, year, engine_HP, msrp): 17 | document = { 18 | 'Make': make, 19 | 'Model': model, 20 | 'Year': year, 21 | 'Engine HP': engine_HP, 22 | 'MSRP': msrp, 23 | 'Date Added': datetime.datetime.now() 24 | } 25 | return cars.insert_one(document) 26 | 27 | def add_customer(first_name, last_name, dob): 28 | document = { 29 | 'First Name': first_name, 30 | 'Last Name': last_name, 31 | 'Date of Birth': dob, 32 | 'Date Added': datetime.datetime.now(), 33 | } 34 | return customers.insert_one(document) 35 | 36 | def add_purchase(car_id, customer_id, method): 37 | document = { 38 | 'Car ID': car_id, 39 | 'Customer ID': customer_id, 40 | 'Method': method, 41 | 'Date': datetime.datetime.now() 42 | } 43 | return purchases.insert_one(document) 44 | 45 | def add_car_data(filename): 46 | with open(filename, 'r') as file: 47 | columns = file.readline().split(',') 48 | file = csv.reader(file) 49 | columns_needed = ['Make', 'Model', 'Year', 'Engine HP', 'Vehicle Size', 'Vehicle Style', 'MSRP'] 50 | indexs = list(filter(lambda x: columns[x].strip() in columns_needed , [i for i in range(len(columns))])) 51 | number_columns = {"MSRP", "Year", "Engine HP"} 52 | 53 | documents = [] 54 | for row in file: 55 | document = {} 56 | for count, index in enumerate(indexs): 57 | data = row[index] 58 | if columns_needed[count] in number_columns: 59 | try: 60 | data = float(data) 61 | except: 62 | continue 63 | document[columns_needed[count]] = data 64 | 65 | documents.append(document) 66 | 67 | cars.insert_many(documents) 68 | 69 | 70 | # QUERIES 71 | 72 | # All cars with price less than 30,000 73 | 74 | result = cars.find({'MSRP': {'$lte': 30000}}) 75 | 76 | result = cars.count_documents({'Make': {'$eq': 'Ford'}}) 77 | 78 | # How many cars are older than 2000 79 | result = cars.count_documents({'Year': {'$lt': 2000}}) 80 | 81 | # Average price of all car brands 82 | result = cars.aggregate([ 83 | {'$group': { 84 | '_id': '$Make', 85 | 'count': {'$sum': 1}, 86 | 'average price': {'$avg': '$MSRP'} 87 | } 88 | } 89 | ]) 90 | #printer.pprint(list(result)) 91 | 92 | # Car with max price 93 | result = cars.aggregate([ 94 | { 95 | '$group': { 96 | '_id': None, 97 | 'max price': {'$max': '$MSRP'} 98 | } 99 | } 100 | ]) 101 | 102 | result = cars.find({}).sort([('MSRP', -1)]).limit(1) 103 | #printer.pprint(list(result)) 104 | 105 | # Average price of hondas newer than 2000 by model 106 | result = cars.aggregate([ 107 | { 108 | '$match': { 109 | 'Make': {'$eq': 'Honda'}, 110 | 'Year': {'$gt': 2000} 111 | } 112 | }, 113 | { 114 | '$group': { 115 | '_id': '$Model', 116 | 'average price': {'$avg': '$MSRP'} 117 | } 118 | } 119 | ]) 120 | 121 | def get_customer_info(): 122 | print('type in your info') 123 | first_name = input('First Name: ') 124 | last_name = input('Last Name: ') 125 | return first_name, last_name 126 | 127 | 128 | first, last = get_customer_info() 129 | customer_and_purchases = list(customers.aggregate([ 130 | { 131 | '$match': {'First Name': first, 'Last Name': last} 132 | }, 133 | { 134 | '$lookup': { 135 | 'from': 'purchases', 136 | 'localField': '_id', 137 | 'foreignField': 'Customer ID', 138 | 'as': 'Purchases' 139 | } 140 | } 141 | ])) 142 | 143 | for i, customer in enumerate(customer_and_purchases): 144 | print(f"{i+1}. {customer['First Name']} {customer['Last Name']}, {customer['Date of Birth']}") 145 | 146 | selection = input('Select a number for the customer: ') 147 | customer = customer_and_purchases[int(selection) - 1] 148 | 149 | print(f'Customer has purchased {len(customer["Purchases"])} cars') 150 | for i, entry in enumerate(customer['Purchases']): 151 | car_id = entry['Car ID'] 152 | car = cars.find_one({'_id': car_id}) 153 | print(f'{i + 1}. {car}') 154 | --------------------------------------------------------------------------------