{ "_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);
}
<?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);
?>
$collection->update(
[],
['$pull' => ['comments' => ['comment' => 'this is my comment']]],
['multiple' => true]
);
$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);
--
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.
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]
);
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/8ad2b76a-9a27-4b09-b95d-af450b9d46ab%40googlegroups.com.