My MongoDB document 'Org_unit' is structured as follows:
{
"_id": ObjectId("0894f016e6e2e073c19e051"),
"allowedusers": [
"admin",
"Fred",
"Bob",
"Layneh"
],
"name": "News management",
"divisions": [
{
"allowedusers": [
"admin",
"Larry",
"Bob",
"Harry",
"Fred"
],
"_id": ObjectId("60894f016e6e2e073c19e052"),
"name": "Finances",
"credentials": [
{
"_id": ObjectId("94f0944f016e6e2e2342cc"),
"name": "FNB - FA",
"userName": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="88edf0e9e5f8e4edc8ede5e9e1e4a6ebe7e5">[email protected]</a>",
"password": "examplePassword!@#"
}
]
},
{
"allowedusers": [
"admin",
"Larry",
"Bob",
"Harry",
"Fred"
],
"_id": ObjectId("60894f016e6e2e073c19e052"),
"name": "Development",
"credentials": [
{
"_id": ObjectId("94f0944f016e6e2e2342cc"),
"name": "FNB - DEV",
"userName": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3f5a475e524f535a7f5a525e5653115c5052">[email protected]</a>",
"password": "examplePassword!@#"
}
]
}
],
"__v": 11
}
Hello everyone, this is my first post here. I'm having trouble accomplishing a task with my database and hoping for some assistance.
Purpose of the Task:
I need to remove "Fred" from the "allowedusers" array within each "division" object in the "divisions" array.
What I Have Attempted:
Despite being new to this field (just about a week in), I've made numerous attempts to solve this issue. The closest I got was with the following code snippet:
Org_unit.updateMany({name: "News management"}, { $pull: {'divisions': {'allowedusers': "Fred"}}},{ multi: true })
However, this code ended up deleting the entire division if "Fred" appeared in its "allowedusers" array.
Any guidance on how to correctly achieve this would be greatly appreciated. Thank you in advance!
PS: Since this is my first question posted, any feedback on how I can improve my questions in the future is welcome!