-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Code for connecting to and inserting into Mongo
- Loading branch information
Showing
2 changed files
with
38 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |