├── 04-ManipulateDataWithACustomUserInterface ├── 03-add_data │ ├── .gitignore │ ├── env_sample.py │ ├── mongo.py │ └── mongo_project.py ├── 02-create_your_menu │ ├── .gitignore │ ├── env_sample.py │ ├── mongo.py │ └── mongo_project.py └── 04-find_update_and_delete_data │ ├── .gitignore │ ├── env_sample.py │ ├── mongo.py │ └── mongo_project.py ├── 03-ManipulateDataProgrammaticallyWithPython └── 01-run_mongo_commands_from_a_python_file │ ├── .gitignore │ ├── env_sample.py │ └── mongo.py ├── 02-ManipulateDataUsingMongoShell ├── 02-update_and_delete_data │ └── mongoCommands.md └── 01-create_and_read_back_data │ └── mongoCommands.md └── 01-CreateAMongoDBDatabase └── 01-create_a_mongodb_database └── mongoSetup.md /04-ManipulateDataWithACustomUserInterface/03-add_data/.gitignore: -------------------------------------------------------------------------------- 1 | env.py 2 | __pycache__/ 3 | -------------------------------------------------------------------------------- /04-ManipulateDataWithACustomUserInterface/02-create_your_menu/.gitignore: -------------------------------------------------------------------------------- 1 | env.py 2 | __pycache__/ 3 | -------------------------------------------------------------------------------- /04-ManipulateDataWithACustomUserInterface/04-find_update_and_delete_data/.gitignore: -------------------------------------------------------------------------------- 1 | env.py 2 | __pycache__/ 3 | -------------------------------------------------------------------------------- /03-ManipulateDataProgrammaticallyWithPython/01-run_mongo_commands_from_a_python_file/.gitignore: -------------------------------------------------------------------------------- 1 | env.py 2 | __pycache__/ 3 | -------------------------------------------------------------------------------- /04-ManipulateDataWithACustomUserInterface/03-add_data/env_sample.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | os.environ.setdefault("MONGO_URI", "mongodb+srv://:@-mongodb.net/?retryWrites=true&w=majority") 4 | -------------------------------------------------------------------------------- /04-ManipulateDataWithACustomUserInterface/02-create_your_menu/env_sample.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | os.environ.setdefault("MONGO_URI", "mongodb+srv://:@-mongodb.net/?retryWrites=true&w=majority") 4 | -------------------------------------------------------------------------------- /04-ManipulateDataWithACustomUserInterface/04-find_update_and_delete_data/env_sample.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | os.environ.setdefault("MONGO_URI", "mongodb+srv://:@-mongodb.net/?retryWrites=true&w=majority") 4 | -------------------------------------------------------------------------------- /03-ManipulateDataProgrammaticallyWithPython/01-run_mongo_commands_from_a_python_file/env_sample.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | os.environ.setdefault("MONGO_URI", "mongodb+srv://:@-mongodb.net/?retryWrites=true&w=majority") 4 | -------------------------------------------------------------------------------- /02-ManipulateDataUsingMongoShell/02-update_and_delete_data/mongoCommands.md: -------------------------------------------------------------------------------- 1 | #### Update the first matching record with nationality == "irish" to have hair_color == "blue": 2 | - `coll.update({nationality: "irish"}, {$set: {hair_color: "blue"}});` 3 | 4 | 5 | #### Update all matching records with nationality == "irish" to have hair_color == "purple": 6 | - `coll.update({nationality: "irish"}, {$set: {hair_color: "purple"}},{multi:true});` 7 | 8 | 9 | #### Delete a record matching: First: "kate", Last: "bush": 10 | - `coll.remove({first: "kate", last: "bush"});` 11 | 12 | 13 | #### Delete all records from the collection: 14 | - `coll.remove();` 15 | -------------------------------------------------------------------------------- /04-ManipulateDataWithACustomUserInterface/03-add_data/mongo.py: -------------------------------------------------------------------------------- 1 | import os 2 | import pymongo 3 | if os.path.exists("env.py"): 4 | import env 5 | 6 | 7 | MONGO_URI = os.environ.get("MONGO_URI") 8 | DATABASE = "myFirstDB" 9 | COLLECTION = "celebrities" 10 | 11 | 12 | def mongo_connect(url): 13 | try: 14 | conn = pymongo.MongoClient(url) 15 | print("Mongo is connected") 16 | return conn 17 | except pymongo.errors.ConnectionFailure as e: 18 | print("Could not connect to MongoDB: %s") % e 19 | 20 | 21 | conn = mongo_connect(MONGO_URI) 22 | 23 | coll = conn[DATABASE][COLLECTION] 24 | 25 | documents = coll.find() 26 | 27 | for doc in documents: 28 | print(doc) 29 | -------------------------------------------------------------------------------- /04-ManipulateDataWithACustomUserInterface/02-create_your_menu/mongo.py: -------------------------------------------------------------------------------- 1 | import os 2 | import pymongo 3 | if os.path.exists("env.py"): 4 | import env 5 | 6 | 7 | MONGO_URI = os.environ.get("MONGO_URI") 8 | DATABASE = "myFirstDB" 9 | COLLECTION = "celebrities" 10 | 11 | 12 | def mongo_connect(url): 13 | try: 14 | conn = pymongo.MongoClient(url) 15 | print("Mongo is connected") 16 | return conn 17 | except pymongo.errors.ConnectionFailure as e: 18 | print("Could not connect to MongoDB: %s") % e 19 | 20 | 21 | conn = mongo_connect(MONGO_URI) 22 | 23 | coll = conn[DATABASE][COLLECTION] 24 | 25 | documents = coll.find() 26 | 27 | for doc in documents: 28 | print(doc) 29 | -------------------------------------------------------------------------------- /04-ManipulateDataWithACustomUserInterface/04-find_update_and_delete_data/mongo.py: -------------------------------------------------------------------------------- 1 | import os 2 | import pymongo 3 | if os.path.exists("env.py"): 4 | import env 5 | 6 | 7 | MONGO_URI = os.environ.get("MONGO_URI") 8 | DATABASE = "myFirstDB" 9 | COLLECTION = "celebrities" 10 | 11 | 12 | def mongo_connect(url): 13 | try: 14 | conn = pymongo.MongoClient(url) 15 | print("Mongo is connected") 16 | return conn 17 | except pymongo.errors.ConnectionFailure as e: 18 | print("Could not connect to MongoDB: %s") % e 19 | 20 | 21 | conn = mongo_connect(MONGO_URI) 22 | 23 | coll = conn[DATABASE][COLLECTION] 24 | 25 | documents = coll.find() 26 | 27 | for doc in documents: 28 | print(doc) 29 | -------------------------------------------------------------------------------- /01-CreateAMongoDBDatabase/01-create_a_mongodb_database/mongoSetup.md: -------------------------------------------------------------------------------- 1 | ### Create Cluster: 2 | - select **Shared Clusters** *(free)* 3 | 4 | 5 | ### Select Cloud Provider: 6 | - select **Amazon Web Services (AWS)** 7 | 8 | 9 | ### Select Region: 10 | - select region closest to you *(any of the free choices)* 11 | 12 | 13 | ### Select Cluster Tier: 14 | - select **M0 Sandbox** *(free forever)* 15 | 16 | 17 | ### Assign Cluster Name: 18 | - any name *(cannot be changed after creation)* 19 | 20 | 21 | ### Create a Database User: 22 | - navigate to **Database Access** under *Security* menu 23 | - SCRAM username and password should only be **alphanumeric** *(no special characters!)* 24 | - select **Read and Write to any Database** from *Database User Privileges* 25 | 26 | 27 | ### Whitelist an IP Address: 28 | - navigate to **Network Access** under *Security* menu 29 | - select **Allow Access From Anywhere** 30 | - *supply IP Addresses of actual hosts further security* 31 | 32 | 33 | ### Create Database on Cluster: 34 | - select **Add My Own Data** from the *Collections* tab on your Cluster Sandbox 35 | - any database name *(camelCase preferred)* 36 | - any collection name *(camelCase preferred)* 37 | 38 | 39 | ### Create New Collection on Database: 40 | - select **Insert Document** within the database 41 | - provide `key/value` pairs 42 | -------------------------------------------------------------------------------- /04-ManipulateDataWithACustomUserInterface/02-create_your_menu/mongo_project.py: -------------------------------------------------------------------------------- 1 | import os 2 | import pymongo 3 | if os.path.exists("env.py"): 4 | import env 5 | 6 | 7 | MONGO_URI = os.environ.get("MONGO_URI") 8 | DATABASE = "myFirstDB" 9 | COLLECTION = "celebrities" 10 | 11 | 12 | def mongo_connect(url): 13 | try: 14 | conn = pymongo.MongoClient(url) 15 | return conn 16 | except pymongo.errors.ConnectionFailure as e: 17 | print("Could not connect to MongoDB: %s") % e 18 | 19 | 20 | def show_menu(): 21 | print("") 22 | print("1. Add a record") 23 | print("2. Find a record by name") 24 | print("3. Edit a record") 25 | print("4. Delete a record") 26 | print("5. Exit") 27 | 28 | option = input("Enter option: ") 29 | return option 30 | 31 | 32 | def main_loop(): 33 | while True: 34 | option = show_menu() 35 | if option == "1": 36 | print("You have selected option 1") 37 | elif option == "2": 38 | print("You have selected option 2") 39 | elif option == "3": 40 | print("You have selected option 3") 41 | elif option == "4": 42 | print("You have selected option 4") 43 | elif option == "5": 44 | conn.close() 45 | break 46 | else: 47 | print("Invalid option") 48 | print("") 49 | 50 | 51 | conn = mongo_connect(MONGO_URI) 52 | coll = conn[DATABASE][COLLECTION] 53 | main_loop() 54 | -------------------------------------------------------------------------------- /03-ManipulateDataProgrammaticallyWithPython/01-run_mongo_commands_from_a_python_file/mongo.py: -------------------------------------------------------------------------------- 1 | import os 2 | import pymongo 3 | if os.path.exists("env.py"): 4 | import env 5 | 6 | 7 | MONGO_URI = os.environ.get("MONGO_URI") 8 | DATABASE = "myFirstDB" 9 | COLLECTION = "celebrities" 10 | 11 | 12 | def mongo_connect(url): 13 | try: 14 | conn = pymongo.MongoClient(url) 15 | print("Mongo is connected") 16 | return conn 17 | except pymongo.errors.ConnectionFailure as e: 18 | print("Could not connect to MongoDB: %s") % e 19 | 20 | 21 | conn = mongo_connect(MONGO_URI) 22 | 23 | coll = conn[DATABASE][COLLECTION] 24 | 25 | documents = coll.find() 26 | 27 | """ Insert a single document """ 28 | # new_doc = { 29 | # "first": "douglas", 30 | # "last": "adams", 31 | # "dob": "11/03/1952", 32 | # "gender": "m", 33 | # "hair_color": "grey", 34 | # "occupation": "writer", 35 | # "nationality": "british" 36 | # } 37 | # coll.insert(new_doc) 38 | 39 | """ Insert multipe documents """ 40 | # new_docs = [{ 41 | # "first": "terry", 42 | # "last": "pratchett", 43 | # "dob": "28/04/1948", 44 | # "gender": "m", 45 | # "hair_color": "not much", 46 | # "occupation": "writer", 47 | # "nationality": "british" 48 | # }, { 49 | # "first": "george", 50 | # "last": "rr martin", 51 | # "dob": "20/09/1948", 52 | # "gender": "m", 53 | # "hair_color": "white", 54 | # "occupation": "writer", 55 | # "nationality": "american" 56 | # }] 57 | # coll.insert_many(new_docs) 58 | 59 | """ Find documents with 'first' name set to 'douglas' """ 60 | # documents = coll.find({"first": "douglas"}) 61 | 62 | """ Delete documents with 'first' name set to 'douglas' """ 63 | # coll.remove({"first": "douglas"}) 64 | # documents = coll.find() 65 | 66 | """ Update a single document (first one only) """ 67 | # coll.update_one( 68 | # {"nationality": "american"}, 69 | # {"$set": {"hair_color": "maroon"}} 70 | # ) 71 | # documents = coll.find({"nationality": "american"}) 72 | 73 | """ Update all documents """ 74 | # coll.update_many( 75 | # {"nationality": "american"}, 76 | # {"$set": {"hair_color": "maroon"}} 77 | # ) 78 | # documents = coll.find({"nationality": "american"}) 79 | 80 | for doc in documents: 81 | print(doc) 82 | -------------------------------------------------------------------------------- /04-ManipulateDataWithACustomUserInterface/03-add_data/mongo_project.py: -------------------------------------------------------------------------------- 1 | import os 2 | import pymongo 3 | if os.path.exists("env.py"): 4 | import env 5 | 6 | 7 | MONGO_URI = os.environ.get("MONGO_URI") 8 | DATABASE = "myFirstDB" 9 | COLLECTION = "celebrities" 10 | 11 | 12 | def mongo_connect(url): 13 | try: 14 | conn = pymongo.MongoClient(url) 15 | return conn 16 | except pymongo.errors.ConnectionFailure as e: 17 | print("Could not connect to MongoDB: %s") % e 18 | 19 | 20 | def show_menu(): 21 | print("") 22 | print("1. Add a record") 23 | print("2. Find a record by name") 24 | print("3. Edit a record") 25 | print("4. Delete a record") 26 | print("5. Exit") 27 | 28 | option = input("Enter option: ") 29 | return option 30 | 31 | 32 | def get_record(): 33 | print("") 34 | first = input("Enter first name > ") 35 | last = input("Enter last name > ") 36 | 37 | try: 38 | doc = coll.find_one({"first": first.lower(), "last": last.lower()}) 39 | except: 40 | print("Error accessing the database") 41 | 42 | if not doc: 43 | print("") 44 | print("Error! No results found.") 45 | 46 | return doc 47 | 48 | 49 | def add_record(): 50 | print("") 51 | first = input("Enter first name > ") 52 | last = input("Enter last name > ") 53 | dob = input("Enter date of birth > ") 54 | gender = input("Enter gender > ") 55 | hair_color = input("Enter hair color > ") 56 | occupation = input("Enter occupation > ") 57 | nationality = input("Enter nationality > ") 58 | 59 | new_doc = { 60 | "first": first.lower(), 61 | "last": last.lower(), 62 | "dob": dob, 63 | "gender": gender, 64 | "hair_color": hair_color, 65 | "occupation": occupation, 66 | "nationality": nationality 67 | } 68 | 69 | try: 70 | coll.insert(new_doc) 71 | print("") 72 | print("Document inserted") 73 | except: 74 | print("Error accessing the database") 75 | 76 | 77 | def main_loop(): 78 | while True: 79 | option = show_menu() 80 | if option == "1": 81 | add_record() 82 | elif option == "2": 83 | print("You have selected option 2") 84 | elif option == "3": 85 | print("You have selected option 3") 86 | elif option == "4": 87 | print("You have selected option 4") 88 | elif option == "5": 89 | conn.close() 90 | break 91 | else: 92 | print("Invalid option") 93 | print("") 94 | 95 | 96 | conn = mongo_connect(MONGO_URI) 97 | coll = conn[DATABASE][COLLECTION] 98 | main_loop() 99 | -------------------------------------------------------------------------------- /02-ManipulateDataUsingMongoShell/01-create_and_read_back_data/mongoCommands.md: -------------------------------------------------------------------------------- 1 | # IDE 2 | - **Connect to Mongo CLI on a IDE** 3 | - navigate to your MongoDB Clusters Sandbox 4 | - click **"Connect"** button 5 | - select **"Connect with the mongo shell"** 6 | - select **"I do not have the mongo shell installed"** 7 | - choose option: **"Run your connection string in your command line"** 8 | - `mongo "mongodb+srv://.mongodb.net/" --username ` 9 | - replace all `` keys with your own data 10 | - enter password *(will not echo ******** *on screen)* 11 | 12 | 13 | #### Clear screen in Mongo Shell: 14 | - `cls` 15 | 16 | 17 | #### Show all database collections: 18 | - `show collections` 19 | 20 | 21 | #### Assign collection to variable 'coll': 22 | - `coll = db.collection_name` 23 | 24 | 25 | #### Insert data to collection: 26 | ```shell 27 | coll.insert({ 28 | first: "john", 29 | last: "lennon", 30 | dob: "09/10/1940", 31 | gender: "m", 32 | hair_color: "brown", 33 | occupation: "beatle", 34 | nationality: "british" 35 | }); 36 | coll.insert({ 37 | first: "eve", 38 | last: "ryan", 39 | dob: "19/09/1992", 40 | gender: "f", 41 | hair_color: "pink", 42 | occupation: "developer", 43 | nationality: "irish" 44 | }); 45 | coll.insert({ 46 | first: "martha", 47 | last: "fenton", 48 | dob: "15/05/1974", 49 | gender: "f", 50 | hair_color: "brown", 51 | occupation: "manager", 52 | nationality: "irish" 53 | }); 54 | coll.insert({ 55 | first: "neil", 56 | last: "hanslem", 57 | dob: "14/07/1983", 58 | gender: "m", 59 | hair_color: "blonde", 60 | occupation: "actor", 61 | nationality: "british" 62 | }); 63 | coll.insert({ 64 | first: "rocky", 65 | last: "persolm", 66 | dob: "19/12/1994", 67 | gender: "f", 68 | hair_color: "black", 69 | occupation: "activist", 70 | nationality: "american" 71 | }); 72 | ``` 73 | 74 | #### Find all documents in collection: 75 | - `coll.find();` 76 | 77 | 78 | #### Find all documents with gender == "f": 79 | - `coll.find({gender: "f"});` 80 | 81 | 82 | #### Find all documents with gender == "f" AND nationality == "british": 83 | - `coll.find({gender: "f", nationality: "british"});` 84 | 85 | 86 | #### Find all documents with gender == "f" AND nationality == "american" OR "irish": 87 | - `coll.find({gender: "f", $or: [{nationality: "american"}, {nationality: "irish"}]});` 88 | 89 | 90 | #### Find all documents with gender == "f" AND nationality == "american" OR "irish", then sort by nationality (ascending): 91 | - `coll.find({gender: "f", $or: [{nationality: "american"}, {nationality: "irish"}]}).sort({nationality: 1});` 92 | 93 | 94 | #### Find all documents with gender == "f" AND nationality == "american" OR "irish", then sort by nationality (descending): 95 | - `coll.find({gender: "f", $or: [{nationality: "american"}, {nationality: "irish"}]}).sort({nationality: -1});` 96 | -------------------------------------------------------------------------------- /04-ManipulateDataWithACustomUserInterface/04-find_update_and_delete_data/mongo_project.py: -------------------------------------------------------------------------------- 1 | import os 2 | import pymongo 3 | if os.path.exists("env.py"): 4 | import env 5 | 6 | 7 | MONGO_URI = os.environ.get("MONGO_URI") 8 | DATABASE = "myFirstDB" 9 | COLLECTION = "celebrities" 10 | 11 | 12 | def mongo_connect(url): 13 | try: 14 | conn = pymongo.MongoClient(url) 15 | return conn 16 | except pymongo.errors.ConnectionFailure as e: 17 | print("Could not connect to MongoDB: %s") % e 18 | 19 | 20 | def show_menu(): 21 | print("") 22 | print("1. Add a record") 23 | print("2. Find a record by name") 24 | print("3. Edit a record") 25 | print("4. Delete a record") 26 | print("5. Exit") 27 | 28 | option = input("Enter option: ") 29 | return option 30 | 31 | 32 | def get_record(): 33 | print("") 34 | first = input("Enter first name > ") 35 | last = input("Enter last name > ") 36 | 37 | try: 38 | doc = coll.find_one({"first": first.lower(), "last": last.lower()}) 39 | except: 40 | print("Error accessing the database") 41 | 42 | if not doc: 43 | print("") 44 | print("Error! No results found.") 45 | 46 | return doc 47 | 48 | 49 | def add_record(): 50 | print("") 51 | first = input("Enter first name > ") 52 | last = input("Enter last name > ") 53 | dob = input("Enter date of birth > ") 54 | gender = input("Enter gender > ") 55 | hair_color = input("Enter hair color > ") 56 | occupation = input("Enter occupation > ") 57 | nationality = input("Enter nationality > ") 58 | 59 | new_doc = { 60 | "first": first.lower(), 61 | "last": last.lower(), 62 | "dob": dob, 63 | "gender": gender, 64 | "hair_color": hair_color, 65 | "occupation": occupation, 66 | "nationality": nationality 67 | } 68 | 69 | try: 70 | coll.insert(new_doc) 71 | print("") 72 | print("Document inserted") 73 | except: 74 | print("Error accessing the database") 75 | 76 | 77 | def find_record(): 78 | doc = get_record() 79 | if doc: 80 | print("") 81 | for k, v in doc.items(): 82 | if k != "_id": 83 | print(k.capitalize() + ": " + v.capitalize()) 84 | 85 | 86 | def edit_record(): 87 | doc = get_record() 88 | if doc: 89 | update_doc = {} 90 | print("") 91 | for k, v in doc.items(): 92 | if k != "_id": 93 | update_doc[k] = input(k.capitalize() + " [" + v + "] > ") 94 | 95 | if update_doc[k] == "": 96 | update_doc[k] = v 97 | 98 | try: 99 | coll.update_one(doc, {"$set": update_doc}) 100 | print("") 101 | print("Document updated") 102 | except: 103 | print("Error accessing the database") 104 | 105 | 106 | def delete_record(): 107 | doc = get_record() 108 | if doc: 109 | print("") 110 | for k, v in doc.items(): 111 | if k != "_id": 112 | print(k.capitalize() + ": " + v.capitalize()) 113 | 114 | print("") 115 | confirmation = input("Is this the document you want to delete?\nY or N > ") 116 | print("") 117 | 118 | if confirmation.lower() == "y": 119 | try: 120 | coll.remove(doc) 121 | print("Document deleted!") 122 | except: 123 | print("Error accessing the database") 124 | else: 125 | print("Document not deleted") 126 | 127 | 128 | def main_loop(): 129 | while True: 130 | option = show_menu() 131 | if option == "1": 132 | add_record() 133 | elif option == "2": 134 | find_record() 135 | elif option == "3": 136 | edit_record() 137 | elif option == "4": 138 | delete_record() 139 | elif option == "5": 140 | conn.close() 141 | break 142 | else: 143 | print("Invalid option") 144 | print("") 145 | 146 | 147 | conn = mongo_connect(MONGO_URI) 148 | coll = conn[DATABASE][COLLECTION] 149 | main_loop() 150 | --------------------------------------------------------------------------------