MongoWriteBatch::execute

(PECL mongo >= 1.5.0)

MongoWriteBatch::executeExécute un lot d'opérations d'écriture

Description

final public MongoWriteBatch::execute ( array $write_options ) : array

Exécute le lot d'opérations d'écriture.

Liste de paramètres

write_options

Voir MongoWriteBatch::__construct.

Valeurs de retour

Retourne un tableau contenant les informations statistiques pour le lot complet. Si le lot a dû être découpé en plusieurs lots, la valeur retournée aggrégera les valeurs des différents lots et ne retournera que le total.

Si le lot est vide, un tableau contenant seulement le champ 'ok' sera retourné (avec la valeur TRUE) même si aucune opération n'a eu lieu (NOOP).

Clé du tableau Signification de la valeur Retourné pour le type de lot
nInserted Nombre de documents insérés MongoWriteBatch::COMMAND_INSERT batch
nMatched Nombre de documents correspondant aux critères de requête MongoWriteBatch::COMMAND_UPDATE batch
nModified Nombre de documents devant être mis à jour MongoWriteBatch::COMMAND_UPDATE batch
nUpserted Nombre de documents insérés MongoWriteBatch::COMMAND_UPDATE batch
nRemoved Nombre de documents supprimés MongoWriteBatch::COMMAND_DELETE batch
ok Indicateur de succès de la commande All

Erreurs / Exceptions

Une exception MongoWriteConcernException est émise en cas d'échec.

Exemples

Exemple #1 Exemple avec MongoWriteBatch::add()

Mise en lot de plusieurs opérations d'insertion

<?php
$mc 
= new MongoClient("localhost");
$collection $mc->selectCollection("test""test");


$docs = array();
$docs[] = array("my" => "demo");
$docs[] = array("is" => "working");
$docs[] = array("pretty" => "well");

$batch = new MongoInsertBatch($collection);
foreach(
$docs as $document) {
    
$batch->add($document);
}
$retval $batch->execute(array("w" => 1));
var_dump($retval);
?>

L'exemple ci-dessus va afficher :

array(2) {
  ["nInserted"]=>
  int(3)
  ["ok"]=>
  bool(true)
}

Exemple #2 Exemple avec MongoWriteBatch::add()

Mise en lot de plusieurs opérations de mise à jour

<?php
$mc 
= new MongoClient("localhost");
$collection $mc->selectCollection("test""test");


$item1 = array(
    
"q" => array("my" => "demo"),
    
"u" => array('$set' => array("try" => 1)),
    
"multi"  => false/* valeur par défaut */
    
"upsert" => false/* valeur par défaut */
);
$item2 = array(
    
"q" => array("is" => "working"),
    
"u" => array('$set' => array("try" => 2)),
    
"multi" => true,
);
$item3 = array(
    
"q" => array("created" => "new-document"),
    
"u" => array('$set' => array("try" => 3)),
    
"upsert" => true,
);

$batch = new MongoUpdateBatch($collection);
$batch->add($item1);
$batch->add($item2);
$batch->add($item3);
$retval $batch->execute(array("w" => 1));
var_dump($retval);

?>

L'exemple ci-dessus va afficher :

array(4) {
  ["nMatched"]=>
  int(18)
  ["nModified"]=>
  int(2)
  ["nUpserted"]=>
  int(0)
  ["ok"]=>
  bool(true)
}

Exemple #3 Exemple avec MongoWriteBatch::add()

Mise en lot de plusieurs opérations d'effacement

<?php
$mc 
= new MongoClient("localhost");
$collection $mc->selectCollection("test""test");


$item1 = array(
    
"q" => array("my" => "demo"),
    
"limit" => 1,
);
$item2 = array(
    
"q" => array("try" => 3),
    
"limit" => 1,
);


$batch = new MongoDeleteBatch($collection);
$batch->add($item1);
$batch->add($item2);
$retval $batch->execute(array("w" => 1));
var_dump($retval);
?>

L'exemple ci-dessus va afficher :

array(2) {
  ["nRemoved"]=>
  int(1)
  ["ok"]=>
  bool(true)
}