How to write Mongo db changeset to remove child node

31 views Asked by At

I have a List of Documents like below which I got it from Mongodb

{
"_id":"some_random_id"
"name": "report1"
"reportDef":
    {
    "reportParameters":
        ["a",
        "b",
        "c",
        "d",
        "e"
        ]
    }
    
"type": "CUSTOM"
"reportId": "desiredReport"
}


{
"_id":"some_random_id"
"name": "report1"
"reportDef":
    {
    "reportParameters":
        ["e",
        "f",
        "b",
        "y",
        "z"
        ]
    }
    
"type": "CUSTOM"
"reportId": "desiredReport"
}

and I need to remove "b" from the reportParameters if this condition satisfies i.e., if type="CUSTOM" and reportId="desiredReport"

means final json document should be like below

{
"_id":"some_random_id"
"name": "report1"
"reportDef":
    {
    "reportParameters":
        ["a",
        "c",
        "d",
        "e"
        ]
    }
    
"type": "CUSTOM"
"reportId": "desiredReport"
}

{
"_id":"some_random_id"
"name": "report1"
"reportDef":
    {
    "reportParameters":
        ["e",
        "f",
        "y",
        "z"
        ]
    }
    
"type": "CUSTOM"
"reportId": "desiredReport"
}

How to add changeset to achieve this?

1

There are 1 answers

1
oneWalker On
db.collection.updateMany(
  {
    "type": "CUSTOM",
    "reportId": "desiredReport"
  },
  {
    "$pull": {
      "reportDef.reportParameters": "b"
    }
  }
);