Skip to content

Commit

Permalink
Add Mongodb indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ru Chern Chong committed May 23, 2024
1 parent a83027f commit 27d961e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
19 changes: 19 additions & 0 deletions db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import os

from dotenv import load_dotenv
from pymongo import MongoClient

load_dotenv()


class MongoDBConnection:
def __init__(self, connection_string=os.environ.get("MONGODB_URI")):
self.client = MongoClient(connection_string)
self.db = self.client[os.environ.get("MONGODB_DB_NAME")]

@property
def database(self):
return self.db

def close_connection(self):
self.client.close()
10 changes: 10 additions & 0 deletions update_cars.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import List

import updater
from db import MongoDBConnection


async def main():
Expand All @@ -12,6 +13,15 @@ async def main():
)
key_fields: List[str] = ["month"]

db = MongoDBConnection().database
collection = db[collection_name]
collection.create_index({"month": 1, "make": 1});
collection.create_index({"make": 1});
collection.create_index({"fuel_type": 1});
collection.create_index({"make": 1, "fuel_type": 1});
collection.create_index({"number": 1});
db.client.close()

response = await updater.main(
collection_name=collection_name,
zip_url=zip_url,
Expand Down
10 changes: 10 additions & 0 deletions update_coe.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import List

import updater
from db import MongoDBConnection


async def main():
Expand All @@ -12,6 +13,15 @@ async def main():
)
key_fields: List[str] = ["month", "bidding_no"]

db = MongoDBConnection().database
collection = db[collection_name]
collection.create_index({"month": 1, "vehicle_class": 1});
collection.create_index({"vehicle_class": 1});
collection.create_index({"month": 1, "bidding_no": 1});
collection.create_index({"premium": 1});
collection.create_index({"bids_success": 1, "bids_received": 1});
db.client.close()

response = await updater.main(
collection_name=collection_name,
zip_url=zip_url,
Expand Down
8 changes: 5 additions & 3 deletions updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from typing import List, Dict, Any

from dotenv import load_dotenv
from pymongo import MongoClient

from db import MongoDBConnection
from download_file import download_file
from utils.create_unique_key import create_unique_key
from utils.extract_zip_file import extract_zip_file
Expand All @@ -18,8 +18,9 @@
async def updater(
collection_name: str, zip_file_name: str, zip_url: str, key_fields: List[str]
) -> str:
client = MongoClient(os.environ.get("MONGODB_URI"))
db = client[os.environ.get("MONGODB_DB_NAME")]
# client = MongoClient(os.environ.get("MONGODB_URI"))
# db = client[os.environ.get("MONGODB_DB_NAME")]
db = MongoDBConnection().database
collection = db[collection_name]

try:
Expand Down Expand Up @@ -53,6 +54,7 @@ async def updater(
start = time.time()
result = collection.insert_many(new_data_to_insert)
end = time.time()
db.client.close()
message = f"{len(result.inserted_ids)} document(s) inserted in {round((end - start) * 1000)}ms"
else:
message = "No new data to insert. The provided data matches the existing records."
Expand Down

0 comments on commit 27d961e

Please sign in to comment.