No cache version.

Caching disabled. Default setting for this page:enabled (code LNG204)
If the display is too slow, you can disable the user mode to view the cached version.

Rechercher une fonction PHP

The MongoWriteBatch class

(PECL mongo >=1.5.0)

Introduction

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.

PHP: MongoWriteBatch - Manual Home of Manuel PHP  Contents Haut

Class synopsis

MongoWriteBatch {
/* Constants */
const int COMMAND_INSERT = 1 ;
const int COMMAND_UPDATE = 2 ;
const int COMMAND_DELETE = 3 ;
/* Methods */
protected __construct ( MongoCollection $collection [, string $batch_type [, array $write_options ]] )
public add ( array $item ) : bool
final public execute ( array $write_options ) : array
}

PHP: MongoWriteBatch - Manual Home of Manuel PHP  Contents Haut

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

PHP: MongoWriteBatch - Manual Home of Manuel PHP  Contents Haut

Description

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.

Note:

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.

PHP: MongoWriteBatch - Manual Home of Manuel PHP  Contents Haut

Errors/Exceptions

PHP: MongoWriteBatch - Manual Home of Manuel PHP  Contents Haut

Examples

Example #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);
?>

The above example will output:

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

PHP: MongoWriteBatch - Manual Home of Manuel PHP  Contents Haut

Table of Contents

Find a PHP function

English translation

You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.

If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.

Thank you in advance.

Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-class.mongowritebatch.html

The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.

References

  1. View the html document Language of the document:fr Manuel PHP : http://php.net

These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.

Contents Haut