Convenience method for a single insert operation


(mongodb >=0.2.0)

MongoDB\Driver\Manager::executeInsertConvenience method for a single insert operation

Описание

final public MongoDB\Driver\WriteResult MongoDB\Driver\Manager::executeInsert ( string $namespace , array|object $document [, MongoDB\Driver\WriteConcern $writeConcern ] )

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

Список параметров

namespace

A fully qualified namespace (databaseName.collectionName)

document

The document to insert.

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.

Ошибки

Примеры

Пример #1 MongoDB\Driver\Manager::executeInsert() example

<?php
$document 
= array(
    
"title" => "mongodb",
);
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY100);

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

printf("Inserted %d document(s)\n"$result->getInsertedCount());

/* 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());
}
?>

Результатом выполнения данного примера будет что-то подобное:

 Inserted 1 document(s) 

Примечания

Замечание:

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).

Смотрите также