PHP/MongoDB problem with aggregation and removal of a comment from a database.

71 views
Skip to first unread message

xgraforlockx

unread,
Sep 2, 2014, 11:19:06 AM9/2/14
to mongod...@googlegroups.com
Hello,

As a beginner I stumbled upon a problem, here's my sample post from the DB :

{
   
"_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")
       
}
   
]
}

What I want to do is, to select that comment id, and then remove() it from my database, I tried to do that in different ways but none of it works.

This is my example get variable url for this purpose
/delete.php?post=design-patterns-vs-frameworks&id=5405ca0eaf105b52133c9869



And here's what I tried using aggregation :

$connect = new MongoClient();
$choqlet
= $connect->selectDB('choqlet');
$entries
= $choqlet->selectCollection('entries');
$comment
= array(    
    array
(
       
'$match' => array(
           
"_id" => $_GET['post']
       
),
   
),
    array
('$unwind' => '$comments'),
    array
(
       
'$match' => array(
           
"comments._id" => $_GET['id']
       
),
   
),
    array
(
       
'$project' => array(
           
"comments" => 1
       
),
   
),
);

$delete
= $entries->aggregate($comment);
$entries
->remove($delete);




But It doesn't select, nor remove anything from the database, how so?

Will Berkeley

unread,
Sep 3, 2014, 2:47:19 PM9/3/14
to mongod...@googlegroups.com
remove() removes documents from a collection. The comment is an embedded document, not a document that is a member of the entries collection. Think of it this way: you are not removing a comment document, you are altering the entry document so that it no longer contains the specific comment embedded document. There's often confusion about documents as members of a collection versus embedded documents. They are entirely different and you can't use find(), update(), remove(), etc to interact with embedded documents except insofar as you are actually interacting with the parent document.

To remove the comment, do the following in the mongo shell (I won't try PHP since I'm not familiar with it even though it looks easy to translate)

db.entries.update({ "_id" : "design-patterns-vs-frameworks" }, { "$pull" : { "comments" : { "_id" : ObjectId("5405ca0eaf105b52133c9869") } } })

Notice we use update() and match the parent doc's _id, and update with an operation to pull the comment from the comments array. We're interacting with the parent entry document, not directly with the comment subdocument.

-Will
Reply all
Reply to author
Forward
0 new messages