title: php mongo Uncaught exception 'MongoDuplicateKeyException' E11000
body: <p>I am trying to migrate data from mysql to mongo. It adds one record fine to mongo but then on the second record I am getting </p>
  
  <pre><code>Fatal error: Uncaught exception 'MongoDuplicateKeyException' with message 'localhost:27017: E11000 duplicate key error index: app.hospitals.$_id_ dup key: { : ObjectId('558365d7423467484bd63af3') }' 
  </code></pre>
  
  <p>Not sure what I am doing wrong</p>
  
  <p>here is my code</p>
  
  <pre><code>&lt;?php
  //echo phpinfo();
  $host = "localhost";
  $user = "root";
  $password = "root";
  $database = "database";
  
  
  // Create connection
  $conn = new mysqli($host, $user, $password, $database);
  
  
  $connection = new MongoClient();
  $db = $connection-&gt;database;
  $collection = $db-&gt;hospitals;
  
  
  
  // Check connection
  if ($conn-&gt;connect_error) {
      die("Connection failed: " . $conn-&gt;connect_error);
  }
  
  $sql = "SELECT * FROM hospitals";
  
  if($result = $conn-&gt;query($sql)){
  
      $i=0;
      while($row = $result-&gt;fetch_assoc()) {
          foreach($row as $key=&gt;$value){
  
              $collection-&gt;insert($row);
              unset($collection-&gt;_id);
          }
          if($i &gt; 3) die;
          $i++;
      }
  
  }
  
  $conn-&gt;close();
  
  ?&gt;
  </code></pre>