How to target object in a double array with positional operator

hello I have an issue with using updateMany and $set operator to update an object in a double array for example I want to do that :
[[ 'a', 'b' ], [ 'c', 'd' ]][[ 'e', 'b' ], [ 'c', 'd' ]]
transform all the a into e like that with a positional operator.
I haven’t find anyone having that issue as they use key but I don’t have any array named until I reach the object I want to update. What is the solution?

Thanks in advance for the help

You can do it like this:

  • $map - to iterate over the parent array
  • $map - to iterate over child array
  • $cond - to check if the child item is equal to “a”. If yes, replace it with “e”. If not, don’t do anything.
db.collection.update({},
[
  {
    "$set": {
      "data": {
        "$map": {
          "input": "$data",
          "as": "item",
          "in": {
            "$map": {
              "input": "$$item",
              "as": "childItem",
              "in": {
                "$cond": {
                  "if": {
                    "$eq": [
                      "$$childItem",
                      "a"
                    ]
                  },
                  "then": "e",
                  "else": "$$childItem"
                }
              }
            }
          }
        }
      }
    }
  }
])

Working example

1 Like