0

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 Answer 1

0
db.collection.updateMany(
  {
    "type": "CUSTOM",
    "reportId": "desiredReport"
  },
  {
    "$pull": {
      "reportDef.reportParameters": "b"
    }
  }
);
Sign up to request clarification or add additional context in comments.

1 Comment

Your answer only contains code. I recommend that you don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.