Skip to content

Commit

Permalink
Code for connecting to and inserting into Mongo
Browse files Browse the repository at this point in the history
  • Loading branch information
audiodude committed Dec 13, 2024
1 parent 4ed3495 commit 25fc8c7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
18 changes: 18 additions & 0 deletions mediabridge/db/connect.py
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
# MongoDB connection setup
import os
from functools import cache

from dotenv import load_dotenv
from pymongo import MongoClient

load_dotenv()


@cache
def connect_to_mongo():
# This will raise an exception if MONGODB_URI is not defined in the environment.
# If that isn't enough information to help developers populate their environment,
# we should use `get` and throw a custom message if the value is missing.
mongo_uri = os.environ["MONGODB_URI"]
client = MongoClient(mongo_uri)
db = client["mediabridge"]
return db
21 changes: 20 additions & 1 deletion mediabridge/db/queries.py
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
# Functions to query MongoDB for movies and interactions
from mediabridge.db.connect import connect_to_mongo


def insert_into_mongo(movie):
db = connect_to_mongo()
collection = db["movies"]
collection.update_one(
{"wikidata_id": movie[1]},
{
"set": {
"netflix_id": movie[0],
"wikidata_id": movie[1],
"title": movie[2],
"year": movie[3],
"genre": movie[4],
"director": movie[5],
}
},
upsert=True,
)

0 comments on commit 25fc8c7

Please sign in to comment.