Now, I have a document in my collection that I would like to delete on click, and those are comments inside the post document.
Here's the structure:
{
"_id" : "design-patterns-vs-frameworks",
"title" : "Design Patterns vs Frameworks",
"category" : [
"Frameworks",
" Design Patterns",
" PHP"
],
"date_time" : ISODate("2014-09-02T13:45:29.124Z"),
"description" : "Frameworks vs Design Patterns",
"content" : " Content!",
"author" : "Maciej Sitko",
"comments" : [
{
"_id" : ObjectId("54061528af105b51133c986c"),
"comment" : "ashfklsfsl\r\n",
"author" : "maciejsitko",
"date" : ISODate("2014-09-02T19:06:16.646Z")
},
{
"_id" : ObjectId("5406152caf105b51133c986d"),
"comment" : "lfasdlfsfl",
"author" : "maciejsitko",
"date" : ISODate("2014-09-02T19:06:20.652Z")
}
]
}
The full delete link is:
delete.php?post=css-clearfix-explained&id=540617e3af105b8d133c986a (as you see two get variables 'post' and 'id')
I tried several solutions,
-First one:
$collection->update( array("_id" => $_GET['post']), array( '$unset' =>
array("comments" => array("_id" => $_GET['id']))) );
This one just removed all of my comments inside the comments array.
-Second:
$delete = array('$pull' => array("comments" => array( '_id' => $_GET['id'])));
$collection->update(array('_id' => $_GET['post']), $delete);
Did not pretty much do anything, strangely enough, this should work, right?
-Third solution:
$collection->remove( array('_id' => $_GET['post'], 'comments._id' => $_GET['id']));
What would be the proper way of achieving this? I struggled on this one a lot, even implementing some aggregation query and it didn't work out.