Reputation: 45360
I am using MongoDB
and GridFS
in PHP
, and trying to figure out how to delete multiple files by the _id
.
Here is the code I have:
$ids = array("50401f40ff558cec38000061", "62401f40ff558cec38000072", "73401f40ff558cec38000083");
$mongo_ids = array();
foreach($ids as $id) {
$mongo_ids[] = new MongoId($id);
}
$mongo_grid_fs->remove(array("_id" => $mongo_ids));
Any idea what I am doing wrong?
Upvotes: 5
Views: 2722
Reputation: 43884
This impossible to do with a single request due to the way that GridFS actually works.
You have two collections:
Inorder to delete a GridFs file you must query BOTH of these table. As such the remove() function actually calls the chunk collection and then removes the file from the files collection.
Since MongoDB cannot, fundamentally, query two collections in one request (joined deleted basically) you must send a delete request per file to delete otherwise you will have left over chunks taking up space in your chunks collection.
As such, taking this into consideration @ToddMoses answer is the correct one.
You can of course use: http://www.php.net/manual/en/mongogridfs.remove.php but I believe it does exactly the same thing, just abstracted so your query should have been:
$mongo_grid_fs->remove(array("_id" => array('$in' => $mongo_ids)));
Upvotes: 3
Reputation: 11039
First, use MongoDB::lastError() to find out what is going wrong. MongoGridFS::remove won’t present you with a message if it fails. DO something like this:
$errorArray = $db->lastError();
var_dump($errorArray);
It appears the problem is that you are not setting the criteria properly. The easiest thing to do is just use Delete instead of Remove since Delete takes an ID as its only parameter:
public bool MongoGridFS::delete ( mixed $id )
This deletes a file from the database whereas remove deletes files from the collection. Since you are looping anyway, you can do something like this:
foreach($ids as $id) {
$mongo_grid_fs->delete($id);
}
Upvotes: 2