From 9ef660cf23f117dddd12ddf77c96a99a54c95091 Mon Sep 17 00:00:00 2001 From: Joel Lord Date: Mon, 12 Sep 2022 14:22:12 -0400 Subject: [PATCH] Changed the code to reflect changes in the article --- pymongo_get_database.py | 17 ++++++ pymongo_test_insert.py | 95 +++++++++++-------------------- pymongo_test_insert_more_items.py | 2 +- pymongo_test_query.py | 31 +++------- 4 files changed, 61 insertions(+), 84 deletions(-) create mode 100644 pymongo_get_database.py diff --git a/pymongo_get_database.py b/pymongo_get_database.py new file mode 100644 index 0000000..cd31273 --- /dev/null +++ b/pymongo_get_database.py @@ -0,0 +1,17 @@ +from pymongo import MongoClient +def get_database(): + + # Provide the mongodb atlas url to connect python to mongodb using pymongo + CONNECTION_STRING = "mongodb+srv://user:pass@cluster.mongodb.net/myFirstDatabase?authSource=admin&replicaSet=atlas-v6xmes-shard-0&w=majority&readPreference=primary&retryWrites=true&ssl=true" + + # Create a connection using MongoClient. You can import MongoClient or use pymongo.MongoClient + client = MongoClient(CONNECTION_STRING) + + # Create the database for our example (we will use the same database throughout the tutorial + return client['user_shopping_list'] + +# This is added so that many files can reuse the function get_database() +if __name__ == "__main__": + + # Get the database + dbname = get_database() diff --git a/pymongo_test_insert.py b/pymongo_test_insert.py index 7d761ad..a636778 100644 --- a/pymongo_test_insert.py +++ b/pymongo_test_insert.py @@ -1,61 +1,34 @@ -def get_database(): - from pymongo import MongoClient - import pymongo - - # Provide the mongodb atlas url to connect python to mongodb using pymongo - CONNECTION_STRING = 'mongodb+srv://:@.mongodb.net/myFirstDatabase' - - # Create a connection using MongoClient. You can import MongoClient or use pymongo.MongoClient - from pymongo import MongoClient - client = MongoClient(CONNECTION_STRING) - - # Create the database for our example (we will use the same database throughout the tutorial - return client['user_shopping_list'] - -# Add this to execute only the called functions from other files -if __name__ == "__main__": - - # Get the database - dbname = get_database() - - # Create a new collection - collection_name = dbname["user_1_items"] - - # Create the first document - item_1 = { - "_id" : "U1IT00001", - "item_name" : "Blender", - "max_discount" : "10%", - "batch_number" : "RR450020FRG", - "price" : 340, - "category" : "kitchen appliance" - } - - # Create the second document - item_2 = { - "_id" : "U1IT00002", - "item_name" : "Egg", - "category" : "food", - "quantity" : 12, - "price" : 36, - "item_description" : "brown country eggs" - } - - # Insert both the documents at once using insert_many() - collection_name.insert_many([item_1,item_2]) - - # Parsing date for the third document - from dateutil import parser - expiry_date = '2021-07-13T00:00:00.000Z' - expiry = parser.parse(expiry_date) - - # Create document 3 - item_3 = { - "item_name" : "Bread", - "quantity" : 2, - "ingredients" : "all-purpose flour", - "expiry_date" : expiry - } - - # Insert single document - collection_name.insert_one(item_3) +from dateutil import parser +# Get the database using the method we defined in pymongo_test_insert file +from pymongo_get_database import get_database +dbname = get_database() +collection_name = dbname["user_1_items"] + +item_1 = { + "_id" : "U1IT00001", + "item_name" : "Blender", + "max_discount" : "10%", + "batch_number" : "RR450020FRG", + "price" : 340, + "category" : "kitchen appliance" +} + +item_2 = { + "_id" : "U1IT00002", + "item_name" : "Egg", + "category" : "food", + "quantity" : 12, + "price" : 36, + "item_description" : "brown country eggs" +} +# collection_name.insert_many([item_1,item_2]) + +expiry_date = '2021-07-13T00:00:00.000Z' +expiry = parser.parse(expiry_date) +item_3 = { + "item_name" : "Bread", + "quantity" : 2, + "ingredients" : "all-purpose flour", + "expiry_date" : expiry +} +collection_name.insert_one(item_3) diff --git a/pymongo_test_insert_more_items.py b/pymongo_test_insert_more_items.py index 31a17e1..1cd3383 100644 --- a/pymongo_test_insert_more_items.py +++ b/pymongo_test_insert_more_items.py @@ -78,6 +78,6 @@ } # Insert all the documents at once -from pymongo_test_insert import get_database +from pymongo_get_database import get_database dbname = get_database() dbname["user_1_items"].insert_many([item_4,item_5,item_6,item_7,item_8,item_9,item_10,item_11,item_12, item_13, item_14]) diff --git a/pymongo_test_query.py b/pymongo_test_query.py index ced2a7b..84f12b9 100644 --- a/pymongo_test_query.py +++ b/pymongo_test_query.py @@ -1,38 +1,25 @@ # Get the database using the method we defined in pymongo_test_insert file -from pymongo_test_insert import get_database +from pandas import DataFrame +from pymongo_get_database import get_database dbname = get_database() # Create a new collection collection_name = dbname["user_1_items"] item_details = collection_name.find() + +# List items without formatting for item in item_details: - # This will give readable output, but KeyError - print(item['item_name'], item['category']) + print(item) -###---------------------------------------------------### -### Comment the above 'for loop' & 'print statements' ### -### for the next lines of code to work ### -###---------------------------------------------------### -from pandas import DataFrame +# This will give readable output, but KeyError +for item in item_details: + print(item['item_name'], item['category']) +# Use pandas for formatting # Convert the dictionary objects to dataframe items_df = DataFrame(item_details) # View all items print(items_df) -###--------------------------------------------------------### -### Get items of particular category without and with index### -###--------------------------------------------------------### -item_details = collection_name.find({"category" : "food"}) -for item in item_details: - print(item) - -# Add more data to understand the need for indexing -import pymongo_test_insert_more_items - -# Create index on category, as an example -category_index = collection_name.create_index("category") - -# Execute the previous query again to see the documents scanned (refer to the article) \ No newline at end of file