Data api updateMany within an array

Hi there,

I’m struggling to figure out a way to update items within an array via the data-api. I’ve got an array (emailVerifyStatus) with 3 items in it and I want to search for all the items named ‘ready’ and change them to ‘uploading’.

  "emailVerifyStatus": [
    "ready",
    "ready",
    "ready"
  ]

I’m using the end point ‘/data/beta/action/updateMany’ with the following code that’s working without an array.

{
   "dataSource":"Cluster0",
   "database":"v2",
   "collection":"allPersons",
   "filter":{
      "host":"hostName",
      "emailVerifier.status":"ready"
   },
   "update":{
      "$set":{
         "emailVerifier.status":"uploading"
      }
   }
}

However this code is not working within an array. Do you have any Advice? Thank you.

Hi,

I assume that’s what your array looks like:

 "emailVerifyStatus": [
    "ready",
    "ready",
    "ready"
  ]

To update all the array elements, use the all positional $[] operator
for example, your $set stage can look like this:

 "$set":{
         "emailVerifier.$[]":"uploading"
      }

if you want to update just matching array elements "emailVerifyStatus":"ready" use the filtered positional operator

{"$set": 
     {"emailVerifyStatus.$[element]":"uploading"}},
     {arrayFilters: [{"element":"ready"}]
}