Update a sub-sub array of a collections

Hi all,
I have a problem for update a sub-sub array…
I have a Collection such as this:

{
    "_id": ObjectId("604c6e86081415566cbf7011")
    "parameters": [
        {
            "_id": ObjectId("602b7455f4b4bf5b41662ec1")
            "name": "Purpose",
            "options": [{
                "id": ObjectId("602b764ff4b4bf5b41662ec2")
                "name": "deb",
                "sel": false,
                "value": null
            }, {
                "id": ObjectId("602b767df4b4bf5b41662ec3")
                "name": "perf",
                "sel": false,
                "value": null
            }, {
                "id": ObjectId("602b764ff4b4bf5b41662ec4")
                "name": "security",
                "sel": false,
                "value": null
            }, {
                "id": ObjectId("602b767df4b4bf5b41662ec5")
                },
                "name": "rel",
                "sel": false,
                "value": null
            }],
            "type": "multiple",
        }, {
            "_id":ObjectId( "602b79d35d4a1333b8b6e5ba")
            "name": "Struct",
            "options": [{
                "id": ObjectId("602b79d353c89933b8238325")
                "name": "SW",
                "sel": false,
                "value": null
            }, {
                "id":ObjectId("602b79d353c89933b8238326")
                "name": "HW",
                "sel": false,
                "value": null
            }],
            "type": "multiple
        }
    ]
}

I need to upgrade one of sub options.

I have try with this command (for example for update Struct options):

.updateOne(
   {
      "_id":ObjectId("604c6e86081415566cbf7011"), 
      "parameters._id:"ObjectId( "602b79d35d4a1333b8b6e5ba")
   },
  {
    $set:{
      "parameters.$.options":newOptions
    }
 })

But doesn’t works.

Do you can suggest another method?

Hi @Francesco_Di_Battist,

Welcome to MongoDB community.

I think what you are looking for is the $[] operator:

https://docs.mongodb.com/manual/reference/operator/update/positional-all/#up.S

.updateOne(
   {
      "_id":ObjectId("604c6e86081415566cbf7011"), 
      "parameters._id:"ObjectId( "602b79d35d4a1333b8b6e5ba")
   },
  {
    $set:{
      "parameters.$[].options":newOptions
    }
 })

Thanks
Pavel

Hi @Pavel_Duchovny, thanks for help me.
I have try it but after this command, newOptions are in all subdocument (not for subdocuments with id 602b79d35d4a1333b8b6e5ba

I suppose that I use $[<identifier>] and arrayFilters that match the paramters._id, but I don’t understand.

Oh I see,

Ok this is how you should do it:

.updateOne(
   {
      "_id":ObjectId("604c6e86081415566cbf7011"), 
      "parameters._id:"ObjectId( "602b79d35d4a1333b8b6e5ba")
   },
  {
    $set:{
      "parameters.$[elem].options":newOptions
    }
 },{arrayFilters : [  { "elem._id": ObjectId( "602b79d35d4a1333b8b6e5ba")} ]})

Hope that I got the syntax right…

Hi @Pavel_Duchovny,
thank you it works.

I doesn’t had understand arrayFilters (i had tried with parameters.elem._id).

Thanks a lot

1 Like

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