Multiple Element Array Write to Database

Hello Folks!

I’m struggling with a specific update command when trying to write to a nested array in my database for a project I’m working on. I am using MongoDB’s Python Driver v3.12. My project works like this:

  1. I first query for a specific document to pull data from that has an array of thingsToDo
  2. I write a function that, with the data I queried with, returns an array with updated info of certain fields in the thingsToDo array.
  3. I am trying to take the fields from the array to do an update_one into mongo.

For more clarity, each document looks something like this:
{ "_id": ObjectId("Plan1"), "thingsToDo": [ { "data" : { coordinates: { lng: 47, lat: 99 }, categories: [restaurant, buffet, point_of_interest] }, "clusterNo": 2, "order": 1, }, { "data" : { coordinates: { lng: 48, lat: 76 }, categories: [musuem, contemporary, point_of_interest] }, "clusterNo": 1, "order": 2, }, ... ] }

And the function outputs something like this:
[ {"order":4,"clusterNo":3}, {"order":3,"clusterNo":2}, ... ]

What I want to achieve is updating the document so that it looks something like this:
{ "_id": ObjectId("Plan1"), "thingsToDo": [ { "data" : { coordinates: { lng: 47, lat: 99 }, categories: [restaurant, buffet, point_of_interest] }, "clusterNo": 4, "order": 3, }, { "data" : { coordinates: { lng: 48, lat: 76 }, categories: [musuem, contemporary, point_of_interest] }, "clusterNo": 3, "order": 2, }, ... ] }

So far, I have gotten confused about the update function. I have read the forums and saw that there can be update, update_one, findandmodify, using some sort of bulk write aggregation, etc. However, I noticed that there is not much explanation on taking a pre-existing array and using that to $set on multiple elements with certain fields in a nested array. Obviously, I cannot just use $ with array filters because each value I have in my output is different. I don’t want to set all elements to one value. Instead, I want to set it to the same position found in the array my function outputs, as well as the position in the thingsToDo field in my document.

I would really appreciate the help and thanks in advance! Please tell me if there is anything else I didn’t clarify.