MongoWriteBatch::execute

(PECL mongo >= 1.5.0)

MongoWriteBatch::execute — Executes a batch of write operations


Описание

final public array MongoWriteBatch::execute ( array $write_options )

Executes the batch of write operations.


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

write_options

- See MongoWriteBatch::__construct.


Возвращаемые значения

Returns an array containing statistical information for the full batch. If the batch had to be split into multiple batches, the return value will aggregate the values from individual batches and return only the totals.

If the batch was empty, an array containing only the 'ok' field is returned (as TRUE) although nothing will be shipped over the wire (NOOP).

Array key Value meaning Returned for batch type
nInsertedNumber of inserted documentsMongoWriteBatch::COMMAND_INSERT batch
nMatchedNumber of documents matching the query criteriaMongoWriteBatch::COMMAND_UPDATE batch
nModifiedNumber of documents actually needed to be modiedMongoWriteBatch::COMMAND_UPDATE batch
nUpsertedNumber of upserted documentsMongoWriteBatch::COMMAND_UPDATE batch
nRemovedNumber of documents removedMongoWriteBatch::COMMAND_DELETE batch
okCommand success indicatorAll


Ошибки

A MongoWriteConcernException exception is thrown on failure.


Примеры

Пример #1 MongoWriteBatch::add() example

Batching up multiple insert operations


$mc = new MongoClient("localhost");
$collection = $mc->selectCollection("test", "test");
$docs = array();
$docs[] = array("my" => "demo");
$docs[] = array("is" => "working");
$docs[] = array("pretty" => "well");
$batch = new MongoInsertBatch($collection);
foreach($docs as $document) {
    $batch->add($document);
}
$retval = $batch->execute(array("w" => 1));
var_dump($retval);

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


array(2) {
["nInserted"]=>
int(3)
["ok"]=>
bool(true)
}

Пример #2 MongoWriteBatch::add() example

Batching up multiple update operations


$mc = new MongoClient("localhost");
$collection = $mc->selectCollection("test", "test");
$item1 = array(
    "q" => array("my" => "demo"),
    "u" => array('$set' => array("try" => 1)),
    "multi"  => false, /* default value */
    "upsert" => false, /* default value */
);
$item2 = array(
    "q" => array("is" => "working"),
    "u" => array('$set' => array("try" => 2)),
    "multi" => true,
);
$item3 = array(
    "q" => array("created" => "new-document"),
    "u" => array('$set' => array("try" => 3)),
    "upsert" => true,
);
$batch = new MongoUpdateBatch($collection);
$batch->add($item1);
$batch->add($item2);
$batch->add($item3);
$retval = $batch->execute(array("w" => 1));
var_dump($retval);

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


array(4) {
["nMatched"]=>
int(18)
["nModified"]=>
int(2)
["nUpserted"]=>
int(0)
["ok"]=>
bool(true)
}

Пример #3 MongoWriteBatch::add() example

Batching up multiple delete operations


$mc = new MongoClient("localhost");
$collection = $mc->selectCollection("test", "test");
$item1 = array(
    "q" => array("my" => "demo"),
    "limit" => 1,
);
$item2 = array(
    "q" => array("try" => 3),
    "limit" => 1,
);
$batch = new MongoDeleteBatch($collection);
$batch->add($item1);
$batch->add($item2);
$retval = $batch->execute(array("w" => 1));
var_dump($retval);

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


array(2) {
["nRemoved"]=>
int(1)
["ok"]=>
bool(true)
}

User Contributed Notes


There are no user contributed notes for this page.

To Top

Описание класса mongowritebatch, примеры использования класса mongowritebatch.



Смотрите также:
Описание на ru2.php.net
Описание на php.ru