(mongodb >=1.0.0)

MongoDB\Driver\BulkWrite::__constructCreate a new BulkWrite


public MongoDB\Driver\BulkWrite::__construct ([ array $options ] )

Constructs a new MongoDB\Driver\BulkWrite, which is a mutable object to which one or more write operations may be added. The write(s) may then be executed with MongoDB\Driver\Manager::executeBulkWrite().


options (array)

Option Type Description Default
bypassDocumentValidation bool

If true, allows insert and update operations to circumvent document level validation.

This option is available in MongoDB 3.2+ and is ignored for older server versions, which do not support document level validation.

ordered bool Ordered operations (true) are executed serially on the MongoDB server, while unordered operations (false) are sent to the server in an arbitrary order and may be executed in parallel. true



Versão Descrição
PECL mongodb 1.1.0 Added the "bypassDocumentValidation" option.


Exemplo #1 MongoDB\Driver\BulkWrite::__construct() example


= new MongoDB\Driver\BulkWrite(['ordered' => true]);
$bulk->insert(['_id' => 1'x' => 1]);
$bulk->insert(['_id' => 2'x' => 2]);
'x' => 2],
'$set' => ['x' => 1]],
'limit' => 1'upsert' => false]
$bulk->delete(['x' => 1], ['limit' => 1]);
'_id' => 3],
'$set' => ['x' => 3]],
'limit' => 1'upsert' => true]

$manager = new MongoDB\Driver\Manager('mongodb://localhost:27017');
$writeConcern = new MongoDB\Driver\WriteConcern(1);

try {
$result $manager->executeBulkWrite('db.collection'$bulk$writeConcern);
} catch (
MongoDB\Driver\Exception\BulkWriteException $e) {
$result $e->getWriteResult();

// Check if the write concern could not be fulfilled
if ($writeConcernError $result->getWriteConcernError()) {
printf("%s (%d): %s\n",
var_export($writeConcernError->getInfo(), true)

// Check if any write operations did not complete at all
foreach ($result->getWriteErrors() as $writeError) {
printf("Operation#%d: %s (%d)\n",
} catch (
MongoDB\Driver\Exception\Exception $e) {
printf("Other error: %s\n"$e->getMessage());

printf("Inserted %d document(s)\n"$result->getInsertedCount());
printf("Updated  %d document(s)\n"$result->getModifiedCount());
printf("Upserted %d document(s)\n"$result->getUpsertedCount());
printf("Deleted  %d document(s)\n"$result->getDeletedCount());


O exemplo acima irá imprimir:

Inserted 2 document(s)
Updated  1 document(s)
Upserted 1 document(s)
Deleted  1 document(s)

Veja Também

add a note add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top