(mongodb >=2.1.0)
MongoDB\Driver\BulkWriteCommand::__construct — Create a new BulkWriteCommand
Constructs a new MongoDB\Driver\BulkWriteCommand, which may be used to perform many insert, update, and delete operations on multiple collections in a single request using the » bulkWrite command introduced in MongoDB 8.0. This differs from MongoDB\Driver\BulkWrite, which is supported by all server versions but limited to a single collection.
After all write operations have been added, this object may be executed with MongoDB\Driver\Manager::executeBulkWriteCommand().
options
(array)
Option | Type | Description | Default |
---|---|---|---|
bypassDocumentValidation | bool |
If |
false |
comment | mixed |
An arbitrary comment to help trace the operation through the database profiler, currentOp output, and logs. |
|
let | array|object |
Dictionnaire des noms et des valeurs des paramètres. Les valeurs doivent être des constantes ou des expressions fermées qui ne font pas référence aux champs du document. Les paramètres peuvent ensuite être accédés en tant que variables dans un contexte d'expression agrégée (par exemple Cette option est disponible dans MongoDB 5.0+ et entraînera une exception au moment de l'exécution si elle est spécifiée pour une version antérieure du serveur. |
|
ordered | bool |
Whether the operations in this bulk write should be executed in
the order in which they were specified. If |
true |
verboseResults | bool |
Whether detailed results for each successful operation should be included in the returned MongoDB\Driver\BulkWriteCommandResult. |
false |
Exemple #1 MongoDB\Driver\BulkWriteCommand::__construct() example
<?php
$manager = new MongoDB\Driver\Manager;
$bulk = new MongoDB\Driver\BulkWriteCommand;
// Delete documents from both collections
$bulk->deleteMany('db.coll_one', []);
$bulk->deleteMany('db.coll_two', []);
// Insert documents into two collections
$bulk->insertOne('db.coll_one', ['_id' => 1]);
$bulk->insertOne('db.coll_two', ['_id' => 2]);
$bulk->insertOne('db.coll_two', ['_id' => 3]);
// Update a document in "coll_one"
$bulk->updateOne('db.coll_one', ['_id' => 1], ['$set' => ['x' => 1]]);
$result = $manager->executeBulkWriteCommand($bulk);
printf("Inserted %d document(s)\n", $result->getInsertedCount());
printf("Updated %d document(s)\n", $result->getModifiedCount());
?>
L'exemple ci-dessus va afficher :
Inserted 3 document(s) Updated 1 document(s)