Skip to content
This repository has been archived by the owner on Sep 19, 2024. It is now read-only.

Changed the code to reflect changes in the article #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions pymongo_get_database.py
Original file line number Diff line number Diff line change
@@ -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:[email protected]/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()
95 changes: 34 additions & 61 deletions pymongo_test_insert.py
Original file line number Diff line number Diff line change
@@ -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://<username>:<password>@<cluster-name>.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)
2 changes: 1 addition & 1 deletion pymongo_test_insert_more_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
31 changes: 9 additions & 22 deletions pymongo_test_query.py
Original file line number Diff line number Diff line change
@@ -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)