Nested array conditional update

II am trying to update specific fields in multilevel array entries that match specific ids. One of the changes has to based on a condition. I have managed to update the static value field correctly but I am not able to apply the conditional change.

the collection items contain a members array, who’s members contain an email_status array, matching the dictionary id, member id, and email_status id, i need to update 2 fields in that specific email_status entry.

document:

{
    _id: ....,
    members: [
         { 
               _id: ....,
              email_statuses: [
                      {
                            _id: ....,
                           prevalent_status: 'SENDING',
                           statuses: {
                                 sent_at: ''
                           }
                      }
              ]
          }
     ]
}
db.collection.updateOne({
  "_id": parentID
},
{
$set: {
	"members.$[e1].email_status.$[e2].statuses.sent_at": "2023-05-22 16:00:00",
	"members.$[e1].email_status.$[e2].prevalent_status": {
		$cond: {
			if: {
				"$members.$[e1].email_status.$[e2].prevalent_status": {
					$nin: [
						"ACCEPTED",
						"DELIVERED",
						"VIEWED",
						"VISITED"
					]
				}
			},
			then: "SENT",
			else: "$members.$[e1].email_status.$[e2].prevalent_status"
		}
	}
}
},
{
"arrayFilters": [
	{
		"e1._id": memberID
	},
	{
		"e2._id": email_statusID
	}
]
})

With the curent code all the $cond block is set as the value of “prevalent_status”

Hi @Dario_Grilli,

To better understand what you are after, can you provide the following information:

  • Several sample documents so that we can easily copy and paste into a test environment. Redact any personal or sensitive information before posting here.
  • MongoDB version in use
  • What error you are getting when you attempted the conditional change? Or does it just not work how you are expecting?

I also noted the following:

The $set you provided references email_status as opposed to email_statuses. Is this a typo?

Look forward to hearing from you.

Regards,
Jason