How to update an entire list, inside nested dictionary in mongo db using pymongo?

Viewed 90

I have the following Document in my mongodb Collection.

{
"user_id": "9d50a8b342f36u75yi90gb0c66cedc0b8b05d570c8e768de0b6fce",
"session_id": "4652478564695355624",
"set_id": "10_models ",
"submit_time": { "2021-09-15T19:20:00.270Z"
},
"est_end_time": null,
"stage": {
    "Start": ["2021-09-15 13:50:06.998395", True],
    "Download": ["0:00:00.463579", True],
    "Model": ["0:00:00.542056", True],
    "End": ["0:00:01.043499", True]
},
"status": "sucess",
"retry": 0
}

My task is to update a stage when its state changes using pymongo. These are my variables the stage and the stage change.

stage_element = "Download"
stage_change = [None, False]

After the update my Document should look like this.

{
"user_id": "9d50a8b342f36u75yi90gb0c66cedc0b8b05d570c8e768de0b6fce",
"session_id": "4652478564695355624",
"set_id": "10_models ",
"submit_time": { "2021-09-15T19:20:00.270Z"
},
"est_end_time": null,
"stage": {
    "Start": ["2021-09-15 13:50:06.998395", True],
    "Download": [None, False],
    "Model": ["0:00:00.542056", True],
    "End": ["0:00:01.043499", True]
},
"status": "sucess",
"retry": 0
}

The values are replaced stage > Download > stage_changed.

I tried the following command but it is not working.

collection.update_one({"$and": [{"session_id": {"$eq": str(session_id)}},
                    {"user_id": {"$eq": str(user_id)}}, {"set_id": {"$eq": str(set_id)}},
                    {"$set": {"stage.str(stage_element).$": stage_change}})

Also, tried the solution provided here and the documentation. But still not able to do it.

0 Answers
Related