File size: 1,346 Bytes
7215d13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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.")