I'm trying to remove a specific array key
from my MongoDB document
.
Structure
{
"_id": "5b4d2548d6bd1de5ba2a7180",
"Fruits": {
"Apple": [{
"weight": "0.502",
"quantity": 1000,
"ts": 1531870931
},
{
"weight": "0.0334",
"quantity": 345,
"ts": 1531870931
}
],
"Banana": [{
"weight": "0.302",
"quantity": 10,
"ts": 1531870931
}]
}
}
I want to remove the element Apple
from the database
.
I found quite similar solutions, but they are all removing one element(s) of an array
(like one entry in Apple
).
There is always only one document
inside my collection
.
PHP Code (not working)
$bulk = new MongoDB\Driver\BulkWrite();
$bulk->update(array(),
array('$pull' => array("Fruits" => array("Apple"))),
["limit" => 1, '_id' => -1, 'upsert' => true]);
$result = $this->db->executeBulkWrite($db . '.' . $collection, $bulk);
Response
0 Modified, Updated, Deleted or Added entries; unchanged DB
Expected Result
{
"_id": "5b4d2548d6bd1de5ba2a7180",
"Fruits": {
"Banana": [{
"weight": "0.302",
"quantity": 10,
"ts": 1531870931
}]
}
}