mongodb - Uncaught exception 'MongoCursorException' with message .... duplicate key error index: -


here's code:

when try work it, there's error it:

 "fatal error: uncaught exception 'mongocursorexception' message  'localhost:27017: e11000 duplicate key error index:    futbol_db.maclar.$kod_1_link_1 dup key:   { : null, : null }' in /var/www/html/cagkansokmen/futbol/db/maclar.php:182    stack trace: #0 /var/www/html/cagkansokmen/futbol/db/maclar.php(182):       mongocollection->insert(array) #1 {main} thrown in    /var/www/html/cagkansokmen/futbol/db/maclar.php on line 182" 

the problem insert. because, when drop insert code, there's no problem code. if try insert data, there's error said.

how can fix it?

$collection1 = $db->lig_kod; $collection_maclar = $db->maclar;     $collection1->ensureindex(array("kod" => 1, "link" => 1), array("unique" => true, "dropdups" => true));      $a = $collection1->find(array())->limit(15)->skip(0);     foreach($a $b){     $kod = $b["kod"];     $link = $b["link"];          $parc = explode(',', $link);         $ligkoduson = $parc[0].",".$parc[1];     $url2 = "http://www.stats.betradar.com/s4/gismo.php?&html=1&id=1424&language=tr&clientid=35&state=".$ligkoduson.",5_".$kod.",9_fixtures,231_fixtures,23_1,242_21&child=0";         $url2 = curl($url2);         $url2 = str_replace("\t","",$url2);         $url2 = str_replace("\n","",$url2);         $url2 = str_replace("\r","",$url2);          $bul = ara("<![cdata[", "]]>", $url2);         $sonuc = ara("setstate('", ",", $bul[0]);         $say = count($sonuc);          for($i = 0; $i<$say; $i++){         $sezonbul = $sonuc[$i];          $lonk = "http://www.stats.betradar.com/s4/gismo.php?&html=1&id=2127&language=tr&clientid=35&state=".$ligkoduson.",".$sezonbul.",9_fixtures,231_full,23_1&child=2";         $fiksturlink = curl("http://www.stats.betradar.com/s4/gismo.php?&html=1&id=2127&language=tr&clientid=35&state=".$ligkoduson.",".$sezonbul.",9_fixtures,231_full,23_1&child=2");          $fiksturlink = str_replace("\t","",$fiksturlink);         $fiksturlink = str_replace("\n","",$fiksturlink);         $fiksturlink = str_replace("\r","",$fiksturlink);          $kategori = ara('title="', '"', $fiksturlink);         $kategori_parcala = explode(' &gt; ', $kategori[0]);         $tur = trim($kategori_parcala[0]);         $ulke = trim($kategori_parcala[1]);             $lig = $kategori_parcala[2];             $lig_parcala = explode(' ', $lig);             $lig_bosluk = count($lig_parcala)-1;         $sezon = trim($lig_parcala[$lig_bosluk]);         $lig_son = trim(str_replace($sezon, "", $lig));               $takimlar = ara('<span class="teams">', '</a>', $fiksturlink);             $timebul = ara('<td class="datetime">', '</td>', $fiksturlink);             $fhbul = ara('<td class="p1 ">', '</td>', $fiksturlink);             $ftbul = ara('<td class="nt ftx ">', '</td>', $fiksturlink);             $dongusay = count($takimlar);         echo $dongusay."<br>";         for($dongu = 0; $dongu<$dongusay; $dongu++){             $takimlarbul = ara('">', '</span>', $takimlar[$dongu]);              $home = trim($takimlarbul[0]);             $away = trim($takimlarbul[2]);                 $time = trim($timebul[$dongu]);                 $time_ayir = explode(' ', $time);                 $yil_ayir = explode('/', $time_ayir[0]);                     $gun = $yil_ayir[0];                     $ay = $yil_ayir[1];                     $yil = $yil_ayir[2];                 $saat_ayir = explode(':', $time_ayir[1]);                     $saat = $saat_ayir[0];                     $dk = $saat_ayir[1];             $time_sonuc = mktime($saat, $dk, 0, $ay, $gun, $yil);             $fh = trim($fhbul[$dongu]);                 if(empty($fh)){                 $fh1 = null;                 $fh2 = null;                 }else{                 $fh_ayir = explode(':', $fh);                 $fh1 = $fh_ayir[0];                 $fh2 = $fh_ayir[1];                 }             $ft = trim($ftbul[$dongu]);             if(empty($ft)){                  $ft1 = null;                 $ft2 = null;              }else{                 if(strpos($ft, '(')){                      $parcala1 = explode('(', $ft);                     $ft_ayir = explode(':', $parcala1[0]);                     $ft1 = $ft_ayir[0];                     $ft2 = $ft_ayir[1];                  }else{                      $ft_ayir = explode(':', $ft);                     $ft1 = $ft_ayir[0];                     $ft2 = $ft_ayir[1];                 }             }         echo $ligkoduson."-".$sezonbul."-".$tur."-".$ulke."-".$lig_son."-".$sezon."-".$home."-".$away."-".$time_sonuc."-".$fh1."-".$fh2."-".$ft1."-".$ft2."<br>";          $collection_maclar->insert(array(         'ulke_kodu'=>$ligkoduson,         'sezon_kodu'=>$sezonbul,         'tur'=>$tur,         'ulke'=>$ulke,         'lig'=>$lig_son,         'sezon'=>$sezon,         'home'=>$home,         'away'=>$away,         'tarih'=>$time_sonuc,         'fh1'=>$fh1,         'fh2'=>$fh2,         'ft1'=>$ft1,         'ft2'=>$ft2         ));         }         }          } 

you have unique index on "kod" , "link", document inserting doesn't include either of these fieldnames.

that means first document insert have these values null, , second 1 too.. fails violoates unique index created.

note "dropdupe" flag provide ensureindex() command means "drop existing duplicates", not "if try use key again, drop previous document".

your current code seems ensuring index on "lig_kod" collection, suspect may have (maybe accidentally) used $collection_maclar variable, rather $collection1 variable , executed code, resulting in creating index on maclar collection.

-hannes


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -