update : 2015.11.03
php.shukuma.com

검색:
 
 
Execute one or more write operations

MongoDB\Driver\Manager::executeBulkWrite

(mongodb >=0.2.0)

MongoDB\Driver\Manager::executeBulkWriteExecute one or more write operations

설명

final public MongoDB\Driver\WriteResult MongoDB\Driver\Manager::executeBulkWrite ( string $namespace , MongoDB\Driver\BulkWrite $bulk [, MongoDB\Driver\WriteConcern $writeConcern ] )

Executes one or more write operations.

A MongoDB\Driver\BulkWrite can be constructed with one or more write operations of varying types (e.g. updates, deletes, and inserts). The driver will attempt to send operations of the same type to the server in as few requests as possible to optimize round trips.

인수

namespace

A fully qualified namespace (databaseName.collectionName)

bulk

The MongoDB\Driver\BulkWrite to execute.

writeConcern

Optionally, a MongoDB\Driver\WriteConcern. If none given, default to the Write Concern set by the MongoDB Connection URI.

반환값

Returns MongoDB\Driver\WriteResult on success, throws exception (instanceof MongoDB\Driver\Exception) on failure.

오류/예외

  • Throws MongoDB\Driver\AuthenticationException if authentication is needed and fails
  • Throws MongoDB\Driver\ConnectionException if connection to the server fails for other then authentication reasons
  • Throws MongoDB\Driver\BulkWriteException on any write failure (WriteError and WriteConcern failures)
  • Throws MongoDB\Driver\RuntimeException on other errors.

예제

Example #1 MongoDB\Driver\Manager::executeBulkWrite() example

<?php
$bulk 
= new MongoDB\Driver\BulkWrite();

$bulk->insert(array("_id" => 1"x" => 1));
$bulk->insert(array("_id" => 2"x" => 2));

$bulk->update(array(  "x" => 2), array('$set' => array("x" => 1)), array("multi" => false"upsert" => false));
$bulk->update(array(  "x" => 3), array('$set' => array("x" => 3)), array("multi" => false"upsert" => true));
$bulk->update(array("_id" => 3), array('$set' => array("x" => 3)), array("multi" => false"upsert" => true));

$bulk->insert(array("_id" => 4"x" => 2));

$bulk->delete(array("x" => 1), array("limit" => 1));


$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY100);
$result $manager->executeBulkWrite("databaseName.collectionName"$bulk$writeConcern);

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

foreach (
$result->getUpsertedIds() as $index => $id) {
    
printf("upsertedId[%d]: "$index);
    
var_dump($id);
}

/* If the WriteConcern could not be fulfilled */
if ($writeConcernError $result->getWriteConcernError()) {
    
printf("%s (%d): %s\n"$writeConcernError->getMessage(), $writeConcernError->getCode(), var_export($writeConcernError->getInfo(), true));
}

/* If a write could not happen at all */
foreach ($result->getWriteErrors() as $writeError) {
    
printf("Operation#%d: %s (%d)\n"$writeError->getIndex(), $writeError->getMessage(), $writeError->getCode());
}
?>

위 예제의 출력 예시:

Inserted 3 document(s)
Matched  1 document(s)
Updated  1 document(s)
Upserted 2 document(s)
Deleted  1 document(s)
upsertedId[3]: object(MongoDB\BSON\ObjectID)#5 (1) {
  ["oid"]=>
  string(24) "54d3adc3ce7a792f4d703756"
}
upsertedId[4]: int(3)