MongoDB\Driver\Manager
PHP Manual

MongoDB\Driver\Manager::executeUpdate

(mongodb >=0.2.0)

MongoDB\Driver\Manager::executeUpdateConvenience method for a single update operation

Descrierea

final public MongoDB\Driver\WriteResult MongoDB\Driver\Manager::executeUpdate ( string $namespace , array|object $filter , array|object $newObj [, array $updateOptions [, MongoDB\Driver\WriteConcern $writeConcern ]] )

Convenience method to execute a MongoDB\Driver\BulkWrite with only one update operation.

Parametri

namespace

A fully qualified namespace (databaseName.collectionName)

filter

The search filter.

newObj

Either an array of update operators, or the full object to be replaced with

updateOptions

updateOptions
Option Type Description Default
multi boolean Update only the first matching document (multi=false), or all matching documents (multi=true) false
upsert boolean If filter does not match an existing document, insert the newObj as a new object, applying any atomic modifiers to the filter if applicable. 0

writeConcern

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

Valorile întoarse

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

Erori/Excepții

Exemple

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

<?php
$criteria 
= array(
    
"tag" => "mongodb",
);
$document = array(
    
'$set' => array(
        
"rating" => 5,
    ),
);
$updateOptions = array(
    
"multi" => true,
);
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY100);

$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$result $manager->executeUpdate("mydb.collection"$criteria$document$updateOptions$writeConcern);

printf("Updated %d document(s)\n"$result->getModifiedCount());
printf("Matched %d document(s)\n"$result->getMatchedCount());
printf("Upserted documents: %d\n"$result->getUpsertedCount());
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"$error->getMessage(), $error->getCode(), var_export($error->getInfo(), true));
}

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

Exemplul de mai sus va afișa ceva similar cu:

Updated 0 document(s)
Matched 0 document(s)
Upserted documents: 0

Example #2 MongoDB\Driver\Manager::executeUpdate() with upsert

<?php
$criteria 
= array(
    
"tag" => "mongodb",
);
$document = array(
    
'$set' => array(
        
"rating" => 5,
    ),
);
$updateOptions = array(
    
"multi" => false,
    
"upsert" => true,
);
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY100);

$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$result $manager->executeUpdate("mydb.collection"$criteria$document$updateOptions$writeConcern);

printf("Updated %d document(s)\n"$result->getModifiedCount());
printf("Matched %d document(s)\n"$result->getMatchedCount());
printf("Upserted documents: %d\n"$result->getUpsertedCount());
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 the write could not happen at all */
foreach ($result->getWriteErrors() as $writeError) {
    
printf("%s (%d)\n"$writeError->getMessage(), $writeError->getCode());
}
?>

Exemplul de mai sus va afișa ceva similar cu:

Updated 0 document(s)
Matched 0 document(s)
Upserted documents: 1
upsertedId[0]: object(BSON\ObjectID)#3 (1) {
  ["oid"]=>
  string(24) "54d2c0d14c245fbe70d32ccf"
}

Note

Notă:

On write failure, MongoDB\Driver\WriteResult::getWriteErrors() will only ever have one MongoDB\Driver\WriteError in the array, and MongoDB\Driver\WriteError::getIndex() will always be 0 (the index of this operation in the batch).

Notă:

MongoDB\Driver\WriteResult::getUpsertedIds() will only ever have one BSON\ObjectID in the array, and the index always be 0 (the index of this operation in the batch).

A se vedea și


MongoDB\Driver\Manager
PHP Manual