#3-> one baseline 2 subs refer the given program code
import json
file_path = "oneBase2Sub"
try:
with open(file_path, "r") as file:
data1 = json.load(file)
# print(data)
data_list = list(data1.items())
# print(data_list)
except Exception as e:
print("Error:", e)
print(len(data_list))
from bson.objectid import ObjectId
not_found_ids = []
not_updated_ids = []
for data_tuple in data_list:
_id, program_code = data_tuple
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, "subscriptionPlan.programCode": program_code})
if subscription_record:
program_start_date = subscription_record.get("startDate", "")
program_code_from_subscription = subscription_record["subscriptionPlan"].get("programCode", "")
result = db["participantBaselineAndFollowupData"].update_one(
{"_id": participant_record["_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: {len(not_found_ids)}")
print(f"IDs not found: {not_found_ids}")
if not_updated_ids:
print(f"IDs not updated: {len(not_updated_ids)}")
print(f"IDs not updated: {not_updated_ids}")
Preview:
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