MongoDB/PHP how can I find and remove an element of document inside array?

Here is my sample (example) database document:

{
    "_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("5405ca0eaf105b52133c9869"),
            "comment" : "my commentary to that futile case.",
            "author" : "maciejsitko",
            "date" : ISODate("2014-09-02T13:45:50.206Z")
        }
    ]
}

As you can see, there’s an array inside that document, and I would like to remove that when user clicks the delete anchor on the previous page. The page where it all happens is (for the one comment listed above):

/delete.php?post=design-patterns-vs-frameworks&id=5405ca0eaf105b52133c9869

So, as you see it gives both post id and then the comment id to delete it.

So, I tried using findandModify() likewise:

$connect = new MongoClient();
$choqlet = $connect->selectDB('choqlet');
$entries = $choqlet->selectCollection('entries');


$delete = $entries->findandModify( array("_id" => $_GET['post'], array( 'comments' => 
 array("_id" => $_GET['id']))), array('remove' => true) );

But it does not remove anything. How can I perform this simple operation?

  1. did you save the changes?
  2. how do you know it didn’t remove the comments?
  3. check out the MongoDB $pull operator.