Preview:
# 2-> 1 subs 1 baseline
import json

file_path = "referOneSub"

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

data_array = data['data']
insert_object_ids = [ObjectId(_id) for _id in data_array]
print(len(insert_object_ids))
# print(insert_object_ids)




from bson.objectid import ObjectId

not_found_ids = []
not_updated_ids = []

for _id in insert_object_ids:
    record = db["participantBaselineAndFollowupData"].find_one({"_id": _id})
    
    if record:
        user_id = record["participantId"]
        
        subscription = db["subscription"].find_one({"userId": user_id})
        
        if subscription:
            program_start_date = subscription.get("startDate", "")
            program_code = subscription["subscriptionPlan"].get("programCode", "")
            
            result = db["participantBaselineAndFollowupData"].update_one(
                {"_id": _id},
                {"$set": {"programCode": program_code, "programStartDate": program_start_date}}
            )
            
            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: {print(not_found_ids)}")

if not_updated_ids:
    print(f"IDs not updated: {len(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