Renaming the Mongolian collection in PHP

The PHP Mongo driver does not have the renameCommand function. There is a link to do this through the admin database. But it seems that more recent versions of the Mongo driver do not allow you to simply "use" the administrator database if you do not have administrator privileges in this database. Thus, this method no longer works. I also read that this does not work in closed environments, although this is currently not a concern.

Another suggestion seems to be to iterate over β€œfrom” the collection and insert β€œinto” into the collection. With proper WriteConcern (fire and forget) it can be pretty fast. But it still means pulling every record over the network into the PHP process, and then uploading it back over the network back to the database.

Ideally, I want it all to be server side. Looks like INSERT INTO ... SELECT ... in SQL. Thus, it is fast, efficient on the net and low loading in PHP.

+7
php mongodb
source share
3 answers

Update

  • Deleted my old map / reduce method since I found out (and pointed out to Sammaye) that this changes the structure
  • Made my version of exec secondary, since I found out how to do this with renameCollection.

I believe that I have found a solution. It seems that some versions of the PHP driver will work with the administrative database, even if it is not needed. But there is a workaround where the authSource connection parameter is used to change this behavior, so it does not authenticate the administrator database, and instead, the database is your choice. So now my renameCollection function is again a wrapper around the renameCollection command.

The key is to add authSource when connecting. In the code below, $ _ENV ['MONGO_URI'] contains my connection string, and default_database_name () returns the name of the database I want to use auth to.

$class = 'MongoClient'; if( !class_exists($class) ) $class = 'Mongo'; $db_server = new $class($_ENV['MONGO_URI'].'?authSource='.default_database_name()); 

Here is my old version that used eval, which should also work, although some environments do not allow you to evaluate ( MongoLab gives you a cripple if you don't have a dedicated system). But if you work in a closed environment, this seems like a smart solution.

 function renameCollection($old_name, $new_name) { db()->$new_name->drop(); $copy = "function() {db.$old_name.find().forEach(function(d) {db.$new_name.insert(d)})}"; db()->execute($copy); db()->$old_name->drop(); } 
0
source share

I just tested this, it works as designed ( http://docs.mongodb.org/manual/reference/command/renameCollection/ ):

 $mongo->admin->command(array('renameCollection'=>'ns.user','to'=>'ns.e')); 

The way you rename a collection without supervision. One problem with MR is that it will change the output form from the original collection. Therefore, copying a collection is not very good. You would be better off copying it manually if your collection is sealed.

As an added note, I upgraded to 1.4.2 (which somehow leaves the pecl channel in phpinfo () as 1.4.3dev :S ), and it still works.

0
source share

you can use this. the dropTarget flag is true and then deletes the existing database.

  $mongo = new MongoClient('_MONGODB_HOST_URL_'); $query = array("renameCollection" => "Database.OldName", "to" => "Database.NewName", "dropTarget" => "true"); $mongo->admin->command($query); 
-one
source share

All Articles