title: Rename mongo database
body: <p>I am doing project using mongodb and php. so here I tried to rename existing database using php. so I did following way to rename database.</p>
  
  <ul>
  <li>first I create new database( user new database name)</li>
  <li>read all records from old db and insert to new db</li>
  <li>then I drop old db</li>
  </ul>
  
  <p>this is my code.</p>
  
  <pre><code>            $conn = new \MongoClient('mongodb://example.com:27017', array("connect" =&gt; TRUE));
              $exist_dbs = $conn-&gt;listDBs();
  
              foreach ($exist_dbs["databases"] as $databse) {
                  if ($databse['name'] == $new_name) {
                      $new_name_is_exist = true;
                  }
              }
              if (!$new_name_is_exist) {
                  $db = new \MongoDB($conn, $old_name);
  
                  //create new database
                  $db_new = new \MongoDB($conn, $new_name);
                  $collections = $db-&gt;getCollectionNames();
  
                  foreach ($collections as $collection) {
                      //create collection
                      $new_collection = new \MongoCollection($db_new, $collection);
  
                      $mongo_collection = $db-&gt;$collection;
                      $objects = $mongo_collection-&gt;find();
  
                      while ($document = $objects-&gt;getNext()) {
                          //add records
                          $new_collection-&gt;insert($document);
                      }
                  }
                  $db-&gt;drop();
                  $msg = 'database renamed';
              } else {
                  $msg = 'given database name already exist';
              }
              $conn-&gt;close();
  </code></pre>
  
  <p>it works fine. but I would like to know is there any better way to rename mongo database using php?</p>