Removing multiple files in MongoDB GridFS in PHP

I am using MongoDBboth GridFSin PHPand trying to figure out how to delete multiple files using _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'm doing wrong?

+5
source share
2 answers

This cannot be done with a single request because GridFS really works.

You have two collections:

  • Files
  • Pieces

To delete a GridFs file, you must query BOTH from this table. Thus, the remove () function actually calls the chunk collection, and then removes the file from the file collection.

MongoDB , , ( ), , , .

, @ToddMoses .

, : http://www.php.net/manual/en/mongogridfs.remove.php, , , , :

$mongo_grid_fs->remove(array("_id" => array('$in' => $mongo_ids)));
+3

MongoDB:: lastError(), , . MongoGridFS:: remove , . - :

$errorArray = $db->lastError();
var_dump($errorArray);

, , . Delete Remove, Delete :

public bool MongoGridFS::delete ( mixed $id )

, . , - :

foreach($ids as $id) {
    $mongo_grid_fs->delete($id);
 }
+2

All Articles