mongodb - php mongo Uncaught exception 'MongoDuplicateKeyException' E11000 -


i trying migrate data mysql mongo. adds 1 record fine mongo on second record getting

fatal error: uncaught exception 'mongoduplicatekeyexception' message 'localhost:27017: e11000 duplicate key error index: app.hospitals.$_id_ dup key: { : objectid('558365d7423467484bd63af3') }'  

not sure doing wrong

here code

<?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->database; $collection = $db->hospitals;    // check connection if ($conn->connect_error) {     die("connection failed: " . $conn->connect_error); }  $sql = "select * hospitals";  if($result = $conn->query($sql)){      $i=0;     while($row = $result->fetch_assoc()) {         foreach($row $key=>$value){              $collection->insert($row);             unset($collection->_id);         }         if($i > 3) die;         $i++;     }  }  $conn->close();  ?> 

using

$collection->save($row); 

instead of insert solved issue. not sure why though.


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -