Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete multiple files in MongoDB GridFS in PHP

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?

like image 861
Justin Avatar asked Aug 31 '12 02:08

Justin


People also ask

What is the use of MongoDB GridFS list?

In MongoDB, use GridFS for storing files larger than 16 MB. In some situations, storing large files may be more efficient in a MongoDB database than on a system-level filesystem. If your filesystem limits the number of files in a directory, you can use GridFS to store as many files as needed.

Which are the two collections of GridFS stores files?

GridFS is divided into two collections: files and chunks. Chunks stores the binary chunks. Use either { files_id: 1, n: 1 } or { files_id: 1 } as the shard key index to shard the chunks collection.

What is bucket in GridFS?

GridFS organizes files in a bucket, a group of MongoDB collections that contain the chunks of files and descriptive information. Buckets contain the following collections, named using the convention defined in the GridFS specification: The chunks collection stores the binary file chunks.


2 Answers

This impossible to do with a single request due to the way that GridFS actually works.

You have two collections:

  • Files
  • Chunks

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)));
like image 141
Sammaye Avatar answered Sep 21 '22 19:09

Sammaye


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);
 }
like image 20
Todd Moses Avatar answered Sep 23 '22 19:09

Todd Moses