MongoWriteBatch::add
(PECL mongo >= 1.5.0)
MongoWriteBatch::add — Adds a write operation to a batch
Description
$item
) : boolAdds a write operation to the batch.
If $item
causes the batch to exceed the
maxWriteBatchSize or maxBsonObjectSize
limits, the driver will internally split the batches into multiple write
commands upon calling MongoWriteBatch::execute().
Parameters
-
item
-
An array that describes a write operation. The structure of this value depends on the batch's operation type.
Batch type Argument expectation MongoWriteBatch::COMMAND_INSERT
The document to add. MongoWriteBatch::COMMAND_UPDATE
Raw update operation.
Required keys are "q" and "u", which correspond to the
$criteria
and$new_object
parameters of MongoCollection::update(), respectively.Optional keys are "multi" and "upsert", which correspond to the "multiple" and "upsert" options for MongoCollection::update(), respectively. If unspecified, both options default to
FALSE
.MongoWriteBatch::COMMAND_DELETE
Raw delete operation.
Required keys are: "q" and "limit", which correspond to the
$criteria
parameter and "justOne" option of MongoCollection::remove(), respectively.The "limit" option is an integer; however, MongoDB only supports 0 (i.e. remove all matching documents) and 1 (i.e. remove at most one matching document) at this time.
Errors/Exceptions
Examples
Example #1 MongoWriteBatch::add() example
Batching up multiple insert operations
<?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);
}
$batch->execute(array("w" => 1));
?>
Example #2 MongoWriteBatch::add() example
Batching up multiple update operations
<?php
$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);
$batch->execute(array("w" => 1));
?>
Example #3 MongoWriteBatch::add() example
Batching up multiple delete operations
<?php
$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);
$batch->execute(array("w" => 1));
?>
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-mongowritebatch.add.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
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.