Preview:
#4-> Refer Subscription based on date.(2 subscription)

import json
from datetime import datetime
from bson.objectid import ObjectId

with open('referSubByDate.json', 'r') as file:
    data = json.load(file)

not_found_ids = []
not_updated_ids = []

for item in data['data']:
    _id = item['id']
    date = item['date']
    date_object = datetime.strptime(date, "%d/%m/%Y")
    new_date = date_object.strftime("%Y-%m-%dT%H:%M:%S.000+00:00")
    
    participant_record = db["participantBaselineAndFollowupData"].find_one({"_id": ObjectId(_id)})
    
    if participant_record:
        user_id = participant_record["participantId"]
        
        subscription_record = db["subscription"].find_one({"userId": user_id, "startDate": new_date})
        
        if subscription_record:
            startDate = subscription_record["startDate"]
            program_code = subscription_record["subscriptionPlan"].get("programCode")
            
            result = db["participantBaselineAndFollowupData"].update_one(
                {"_id": participant_record["_id"]},
                {"$set": {"programCode": program_code, "programStartDate": startDate}}
            )
            
            if result.modified_count == 0:
                not_updated_ids.append(_id)
        else:
            not_updated_ids.append(_id)
    else:
        not_found_ids.append(_id)

print("Completed")

if not_found_ids:
    print(f"IDs not found: {not_found_ids}")

if not_updated_ids:
    print(f"IDs not updated: {not_updated_ids}")

        
    

downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter