Batch クラス群

目次

The MongoWriteBatch class

はじめに

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
class MongoWriteBatch {
/* 定数 */
const int MongoWriteBatch::COMMAND_INSERT = 1 ;
const int MongoWriteBatch::COMMAND_UPDATE = 2 ;
const int MongoWriteBatch::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

MongoWriteBatch::COMMAND_INSERT

Create an Insert Write Batch

MongoWriteBatch::COMMAND_UPDATE

Create an Update Write Batch

MongoWriteBatch::COMMAND_DELETE

Create an Delete Write Batch

説明

When executing a batch by calling MongoWriteBatch::execute, MongoWriteBatch will send up to maxWriteBatchSize (defaults to 1000) documents or 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.

エラー / 例外

  • Exception on parameter parsing failures
  • Exception on argument validation errors (e.g. missing keys)
  • MongoProtocolException when talking to MongoDB server older then 2.6.0.
  • MongoProtocolException on socket errors.
  • MongoWriteConcernException when a write fails due to WriteConcerns

例1 MongoWriteBatch example

Adding documents to a Insert batch and then execute it

<?php
$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)
}

The MongoInsertBatch class

はじめに

Constructs a batch of INSERT operations. See MongoWriteBatch.

クラス概要

MongoInsertBatch
class MongoInsertBatch extends MongoWriteBatch {
/* メソッド */
public __construct ( MongoCollection $collection [, array $write_options ] )
/* 継承したメソッド */
public bool MongoWriteBatch::add ( array $item )
final public array MongoWriteBatch::execute ( array $write_options )
}

The MongoUpdateBatch class

はじめに

Constructs a batch of UPDATE operations. See MongoWriteBatch.

クラス概要

MongoUpdateBatch
class MongoUpdateBatch extends MongoWriteBatch {
/* メソッド */
public __construct ( MongoCollection $collection [, array $write_options ] )
/* 継承したメソッド */
public bool MongoWriteBatch::add ( array $item )
final public array MongoWriteBatch::execute ( array $write_options )
}

The MongoDeleteBatch class

はじめに

Constructs a batch of DELETE operations. See MongoWriteBatch.

クラス概要

MongoDeleteBatch
class MongoDeleteBatch extends MongoWriteBatch {
/* メソッド */
public __construct ( MongoCollection $collection [, array $write_options ] )
/* 継承したメソッド */
public bool MongoWriteBatch::add ( array $item )
final public array MongoWriteBatch::execute ( array $write_options )
}