MongoDB\Driver\BulkWrite
PHP Manual

MongoDB\Driver\BulkWrite::__construct

(mongodb >=0.2.0)

MongoDB\Driver\BulkWrite::__constructCreate new BulkWrite

Description

public MongoDB\Driver\BulkWrite::__construct ([ boolean $ordered = true ] )

Constructs a new ordered, or unordered MongoDB\Driver\BulkWrite.

Parameters

ordered

Ordered bulkes (TRUE) are executed serially on the MongoDB server, while unorderd bulkes (FALSE) are sent to the server in arbitrary order and may be executed in parallel.

Errors/Exceptions

Examples

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

<?php

$bulk 
= new MongoDB\Driver\BulkWrite(true);
$bulk->insert($a);
$bulk->insert($b);
$bulk->update($c$obj);
$bulk->update($d$obj);
$bulk->delete($e);
$bulk->delete($f);
$bulk->update($g$obj);
$bulk->delete($h);

$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY1000);
try {
    
$result $manager->executeBulkWrite("databaseName.collectionName"$bulk$writeConcern);
} catch(
MongoDB\Driver\DuplicateKeyException $ex) {
    
$result $ex->getWriteResult();
    
/* If the WriteConcern could not be fullfilled */
    
if ($writeConcernError $result->getWriteConcernError()) {
        
printf("%s (%d): %s\n",
            
$writeConcernError->getMessage(),
            
$writeConcernError->getCode(),
            
var_export($writeConcernError->getInfo(), true)
        );
    }

    
/* If the write could not happen at all */
    
foreach ($result->getWriteErrors() as $writeError) {
        
printf("Operation#%d: %s (%d)\n",
            
$writeError->getIndex(),
            
$writeError->getMessage(),
            
$writeError->getCode()
        );
    }
} catch(
MongoDB\Driver\AuthenticationException $ex) {
    
printf("Cannot authenticate: %s\n"$ex->getMessage());
    exit(
1);
} catch(
MongoDB\Driver\ConnectionException $ex) {
    
printf("Cannot connect to MongoDB server: %s\n"$ex->getMessage());
    exit(
2);
} catch(
MongoDB\Driver\RuntimeException $ex) {
    
printf("Unknown error: %s\n"$ex->getMessage());
    exit(
3);
}

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

The above example will output:

Inserted 2 document(s)
Updated  3 document(s)
Deleted  9 document(s)

See Also


MongoDB\Driver\BulkWrite
PHP Manual