Here is your MongoDB answer pull the array element from the collection
To remove a specific element from an array of some document, you first need to identify that element. For example, I have a document with an array, and each element of this array is an object:
`{
"_id": ObjectId ("5140f34888dd50971900002d"),
"_permissions": {
"edit": [
{
"profile_id": NumberLong (123),
"comment": "app / project owner"
},
{
"profile_id": NumberLong ("153579099841888257"),
"comment": "project admin"
},
{
"profile_id": NumberLong ("153579095869882369"),
"comment": "project admin"
}
],
"view": [
{
"profile_id": NumberLong (123),
"comment": "app / project owner"
},
{
"profile_id": NumberLong ("153579099841888257"),
"comment": "project admin"
},
{
"profile_id": NumberLong ("153579095869882369"),
"comment": "project admin"
}
]
}
} `
So, let's remove "profile_id" with "153579099841888257" from the _permissions.view array. Here we go
`db.collection.update({_id: ObjectId("5140f34888dd50971900002d")}, {$pull:{"_permissions.view": {"profile_id": NumberLong("153579099841888257")}}});`
- I define the scope of the object (to make sure the identifier does not affect any other document found)
- Define the required output element: "profile_id" with the value "153579099841888257" in the _permissions.view array
source share