PHP Velho Oeste 2024

La clase MongoDuplicateKeyException

(PECL mongo >= 1.5.0)

Introducción

Lanzada al intentar insertar un documento en una collección que ya contiene los mismos valores para las claves únicas.

Sinopsis de la Clase

MongoDuplicateKeyException extends MongoWriteConcernException {
/* Propiedades heredadas */
protected string $message ;
protected int $code ;
protected string $file ;
protected int $line ;
/* Métodos heredados */
}

Ejemplos

Ejemplo #1 Capturar una MongoDuplicateKeyException

<?php
$mc 
= new MongoClient("localhost");

$c $mc->selectCollection("test""test");

$c->insert(array('_id' => 1));
try {
    
$c->insert(array('_id' => 1));
} catch (
MongoWriteConcernException $e) {
    echo 
$e->getMessage(), "\n";
}
?>

El resultado de los ejemplos sería algo similar a:

localhost:27017: insertDocument :: caused by :: 11000 E11000 duplicate key error index: test.test.$_id_  dup key: { : 1 }
add a note add a note

User Contributed Notes 2 notes

up
0
fastest963 at gmail dot com
9 years ago
Not sure about earlier versions but this definitely exists in pecl mongo 1.2.10.
up
-1
sarahjay dot wth at gmail dot com
5 years ago
Multiplication:

<!DOCTYPE html>
<html>
<body>

<?php
$x
= 10
$y = 6;

echo
$x * $y;
?> 

</body>
</html>

See More details on: http://bizmaa.com
To Top