`I’m working with FastAPI to create database migrations for adding columns to my MongoDB collections. Before running my change stream script, I noticed a recommendation to set a flag called changestreams_verified to true.
Can anyone explain why it’s necessary to set changestreams_verified to true before executing the change stream script? What potential issues could arise if this step is skipped, and how does it impact the behavior of the change streams?
Here’s a snippet of the relevant code:
@retry(stop=stop_after_attempt(15), retry=retry_if_exception_type(WriteError))
async def add_field_to_organization():
await organization_collection.update_many(
{"task_types": {"$exists": False}},
{
"$set": {
"task_types": [
{"name": "Default", "description": "default task type"},
{"name": "Oversight", "description": "Oversight"},
]
}
},
)
async def run_script(input_params: dict = {}):
# Update all organizations to create task_types fields
try:
await add_field_to_organization()
result = await organization_collection.count_documents(
{
"task_types": [
{"name": "Default", "description": "default task type"},
{"name": "Oversight", "description": "Oversight"},
]
}
)
return f"Successfully added task_types field for {result} organization."
except Exception:
error = "Error while modifying organization"
logging.exception(error)
return error
I tried running the change stream script without setting changestreams_verified to true, but I encountered some inconsistencies and unexpected behaviors in the change stream events. Specifically, I got an error, and the migration did not run as expected. I was expecting the change stream to reliably capture and handle all changes in the database collection.
Any insights or detailed explanations would be greatly appreciated!`
Satya Prakash is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.