TestProto / pymongo_migration.py
DeveshThakran's picture
Upload 10 files
7215d13 verified
Raw
History Blame Contribute Delete
1.35 kB
from pymongo import MongoClient
# MongoDB connection
mongo_db_uri = "mongodb+srv://AhumMongoDB:MongoDBAhum@ahumcluster.64iflwq.mongodb.net/?retryWrites=true&w=majority&appName=Ahumcluster"
client = MongoClient(mongo_db_uri)
db = client["Quotes"]
collection = db["Ahum"]
# Loop through each document
for doc in collection.find():
full_text = doc.get("text", "")
# Try to split the quote and author using dash variations
if "–" in full_text:
quote_part, author = full_text.rsplit("–", 1)
elif "-" in full_text:
quote_part, author = full_text.rsplit("-", 1)
else:
quote_part, author = full_text, "Unknown"
# Clean up punctuation and whitespace
quote_cleaned = quote_part.strip(' “”"\'').strip()
author_cleaned = author.strip(' “”"\'').strip()
# Convert tags to capitalized "emotions"
tags = doc.get("tags", [])
emotions = [tag.capitalize() for tag in tags]
# Update the document
collection.update_one(
{"_id": doc["_id"]},
{"$set": {
"quote": quote_cleaned,
"author": author_cleaned,
"emotions": emotions
},
"$unset": {
"text": "", # remove old field
"tags": "" # remove old field
}}
)
print("✅ All documents updated successfully.")