Наш чат в Telegram для обмена идеями, проектами, мыслями, людьми в сфере ИТ г.Ростова-на-Дону: @it_rostov

The MongoWriteBatch class

(PECL mongo >=1.5.0)


MongoWriteBatch is the base class for the MongoInsertBatch, MongoUpdateBatch and MongoDeleteBatch classes.

MongoWriteBatch allows you to "batch up" multiple operations (of same type) and shipping them all to MongoDB at the same time. This can be especially useful when operating on many documents at the same time to reduce roundtrips.

Prior to version 1.5.0 of the driver it was possible to use MongoCollection::batchInsert(), however, as of 1.5.0 that method is now discouraged.

Note: This class is only available when talking to MongoDB 2.6.0 (and later) servers. It will throw MongoProtocolException if attempting to use it on older MongoDB servers.

Обзор классов

MongoWriteBatch {
/* Константы */
const int COMMAND_INSERT = 1 ;
const int COMMAND_UPDATE = 2 ;
const int COMMAND_DELETE = 3 ;
/* Методы */
protected __construct ( MongoCollection $collection [, string $batch_type [, array $write_options ]] )
public bool add ( array $item )
final public array execute ( array $write_options )

MongoWriteBatch types


- Create an Insert Write Batch


- Create an Update Write Batch


- Create an Delete Write Batch


When executing a batch, by calling MongoWriteBatch::execute(), MongoWriteBatch will send over » maxWriteBatchSize (defaults to 1000) documents or up to » maxBsonObjectSize (defaults to 16777216 bytes), whichever comes first.


Documents will never be partially transferred. When adding documents to the batch, that overflows the limit, a new batch will be created and the document put into the new batch.



Пример #1 MongoWriteBatch example

Adding documents to a Insert batch and then execute it

$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) {
$retval = $batch->execute(array("w" => 1));

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

array(2) {


User Contributed Notes

There are no user contributed notes for this page.

To Top

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

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