Can't update in nested arrays

Hey,

I’ve been trying to modify a value in multiple arrays for a few arrays and I can’t find documentation on how to do this.

My collection looks like this

"rates": [
    {
      "category": "Web",
      "seniorityRates": [
        {
          "seniority": "junior",
          "rate": 100
        },
        {
          "seniority": "intermediate",
          "rate": 135
        },
        {
          "seniority": "senior",
          "rate": 165
        }
      ]
    }
  ]

I’m just trying to modify “junior” to “beginner”, this should be simple.

Thanks to these answers:

https://stackoverflow.com/questions/54055702/how-can-i-update-a-multi-level-nested-array-in-mongodb

https://stackoverflow.com/questions/9611833/mongodb-updating-fields-in-nested-array

I’ve manage to write that python code (pymongo), but it doesn’t works…

result = my_coll.update_many({},
        {
            "$set":
            {
                "rates.$[].seniorityRates.$[j].seniority" : new
            }
        },
        upsert=False,
        array_filters= [
                {
                "j.seniority": old
                }
            ]
        )

The path ‘rates’ must exist in the document in order to apply array updates.

It correspond to this command that doesn’t work either

db.projects.updateMany({},
  {
      $set:
      {
          "rates.$[].seniorityRates.$[j].seniority" : "debutant"
      }
  },
  { arrayFilters = [
          {
          "j.seniority": "junior"
          }
      ]
  }
)

clone(t={}){const r=t.loc||{};return e({loc:new Position("line"in r?r.line:this.loc.line,"column"in r?r.column:……)} could not be cloned

What am I doing wrong ?

Any help would be very appreciated

The above might hint at the fact that some documents do not have the rates array.

I would try to update only document that matches

{"rates.0":{"$exists":true}}
1 Like

Hey,

so it was indeed simple, thanks steevej for helping me. As all docs had “rates” I thought it may not be the correct collection. And in pymongo I inverted two paramaters (so dumb to have spent this much time on that).

But I wanted to try in mongo to avoid this kind of mistakes and it was not working either so I thought that the query was wrong.

—> found later that the mongo query was wrong because I wrote " arrayFilters= " instead of “:”

it’s a bit shameful…

3 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.