File size: 2,165 Bytes
adcbb74 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi
import datetime
import os
import uuid
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
uri = os.getenv("MONGO_URL")
# β
Create Mongo client
client = MongoClient(uri, server_api=ServerApi('1'))
# β
Ping test
try:
client.admin.command('ping')
print("β
Connected to MongoDB!")
except Exception as e:
print("β MongoDB connection failed:", e)
# β
Database & Collection
db = client["ReasoningData"]
collection = db["formatted_data"]
# β
Global trackers
global_question_list = []
counter = 0
def generate_unique_id():
"""
Generates a unique ID for each question.
Example: ODR_20250822_123456_uuid
"""
global counter
prefix = "ODR"
timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
unique_id = f"{prefix}_{timestamp}_{counter:05d}"
counter = (counter + 1) % 100000
return unique_id
def convert_into_mongo_document(parsed_json):
"""
Converts parsed Pydantic object (or dict) into Mongo document.
"""
if hasattr(parsed_json, "dict"): # if it's a Pydantic model
parsed_json = parsed_json.dict()
data = {
"question_id": generate_unique_id(),
"question_content": parsed_json.get("question_content"),
"answer_language": parsed_json.get("answer_language"),
"reasoning_content": parsed_json.get("reasoning_content"),
"answer_content": parsed_json.get("answer_content"),
}
return data
def insert_into_mongo(data):
"""
Inserts a document into MongoDB.
"""
try:
data["_id"] = data["question_id"]
result = collection.insert_one(data)
global_question_list.append(result.inserted_id)
print("β
Inserted document ID:", result.inserted_id)
except Exception as e:
print("β Error inserting document:", e)
def db_pipelining(parsed_json):
"""
Full pipeline: convert β insert.
"""
data = convert_into_mongo_document(parsed_json)
insert_into_mongo(data)
|