Delete on document with php

42 views
Skip to first unread message

Anis Mrad

unread,
Jul 31, 2015, 7:04:37 PM7/31/15
to mongodb-user
Hello, 

I have this collection : articles 

{ "_id" : ObjectId("55bbf8750faaf2af06b7acdb"), "title" : "test", "content" : "hello", "utilisateur" : "anis", "saved_at" : ISODate("2015-07-31T22:36:37.863Z"), "comments" : [ { "comment" : "this is my comment", "user" : "anis" } ] }

And i want to delete for example the comment : "this is my comment". I know with the terminal window we make this : 

db.articles.update( { }, { "$pull": { "comments": { "comment" : "this is my comment" } } }, { "multi": true } )

But i want to make it with php i have start but my function is not work, when i sue this function the comment are not removed. I think the problem is $query.


function commentdelete() {
 
try

 
{

 $connection
= new MongoClient();

 $database
= $connection->selectDB('Monblog');

 
}
 
Catch(MongoException $e)
 
{

 
die("Failed to connect to database " . $e->getMessage());

 
}

 $collection
= $database->articles;

 $query
=array( 'comment' => $_SESSION['user']);

 $cursor
=$collection->find($query);

 $collection
->remove($query);


}



This is my file for add comment :

 
<?php
session_start
();
?>
<?php
         $id
= $_POST['article_id'];
         
try {
           $mongodb
= new MongoClient();
           $collection
= $mongodb->Monblog->articles;
         
} catch (MongoConnectionException $e) {
           
die('Failed to connect to MongoDB '.$e->getMessage());
         
}
         $article
= $collection->findOne(array('_id' => new MongoId($id)));
         $comment
= array(
           
           
'comment' => $_POST['comment'],
           
"user"=>$_SESSION["user"],
         
         
);


         $collection
->update(array('_id' => new MongoId($id)),
           array
('$push' => array('comments' => $comment)));
         header
('Location: blog.php?id='.$id);
         
 
?>    

    Thanks to help me

Jeremy Mikola

unread,
Aug 1, 2015, 5:54:22 PM8/1/15
to mongod...@googlegroups.com
The equivalent of your $pull update in PHP would be:

$collection->update(
  [],
  ['$pull' => ['comments' => ['comment' => 'this is my comment']]],
  ['multiple' => true]
);

Here's some feedback on the first code snippet you shared (comments mine):

$query=array( 'comment' => $_SESSION['user']);

// This line is essentially dead code. You construct a MongoCursor, but the query is never executed because iteration never occurs
$cursor = $collection->find($query);

// Here, you're attempting to remove documents where the comment field equals the $_SESSION['user'] value, which doesn't make sense given your schema
$collection->remove($query);

Also, selecting a database or collection does not need to be included in the try/catch block. The MongoClient construction would throw the exception, as that is what attempts to connect to the database server. Creating MongoDB or MongoCollection objects do not involve any client-to-server communication.


--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/69f7fab2-6fb6-43b9-af7c-87c03286c0a8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Anis Mrad

unread,
Aug 1, 2015, 6:32:14 PM8/1/15
to mongodb-user
thanks for your help so if i make this its correct ?

 function commentdelete() {
 
try


 
{


 $connection
= new MongoClient();


 $database
= $connection->selectDB('Monblog');


 
}
 
Catch(MongoException $e)
 
{


 
die("Failed to connect to database " . $e->getMessage());


 
}


 $collection
= $database->articles;



$collection
->update(
 
[],
 
['$pull' => ['comments' => ['comment' => $_SESSION["user"]]]],
 
['multiple' => true]
);

Jeremy Mikola

unread,
Aug 2, 2015, 1:26:29 PM8/2/15
to mongod...@googlegroups.com
Your update query is also attempting to delete comments from all documents in the articles collection where the "comment" field equals $_SESSION["user"]. I don't know exactly what $_SESSION["user"] contains, but I assume that field should be matched against the embedded comment's "user" field. But even if you changed to update to pull all comments where e "user" field equals $_SESSION["user"], do you really mean to delete all comments from all articles?

If you were intending to create a function that removes a single comment, I would suggest adding some unique value to your embedded comment documents. For instance, you can add an extra "id" field to each embedded document and store an ObjectId (i.e. MongoId in PHP), which would give you some distinct value to $pull on. On the same token, I would also suggest adding the article's "_id" to your update criteria if you do intend to do a single-comment deletion. Currently, you're issuing the update against all documents in the articles collection -- which is fine if you're intending to delete all comments from a single user, but overkill if you're just trying to delete one comment in a single article.


Anis Mrad

unread,
Aug 2, 2015, 3:33:05 PM8/2/15
to mongodb-user
thanks for your reply :)
Reply all
Reply to author
Forward
0 new messages