1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
--TEST--
MongoUpdateBatch::execute() Updating one and multiple documents in the same batch
--DESCRIPTION--
This tests batches consisting of multiple update operations. That means that
update result merging is also implicitly tested.
--SKIPIF--
<?php $needs = "2.5.5"; ?>
<?php if (!MONGO_STREAMS) { echo "skip This test requires streams support"; } ?>
<?php if ( ! class_exists('MongoWriteBatch')) { exit('skip This test requires MongoWriteBatch classes'); } ?>
<?php require_once "tests/utils/standalone.inc" ?>
--FILE--
<?php
require_once "tests/utils/server.inc";
$numBatches = 0;
function log_write_batch($server, $write_options, $batch, $protocol_options) {
global $numBatches;
$numBatches += 1;
}
$ctx = stream_context_create(array(
'mongodb' => array('log_write_batch' => 'log_write_batch'),
));
$host = MongoShellServer::getStandaloneInfo();
$mc = new MongoClient($host, array(), array('context' => $ctx));
$collection = $mc->selectCollection(dbname(), collname(__FILE__));
$collection->drop();
$collection->insert(array('_id' => 1, 'foo' => 'bar'));
$collection->insert(array('_id' => 2, 'foo' => 'bar'));
$collection->insert(array('_id' => 3, 'foo' => 'bar'));
$collection->insert(array('_id' => 4, 'foo' => 'bar'));
$batch = new MongoUpdateBatch($collection);
printf("Updating one document (multi unspecified) as batch op #1\n");
$update = array(
'q' => array('foo' => 'bar'),
'u' => array('$set' => array('foo' => 'abc')),
);
var_dump($batch->add($update));
printf("Updating one document (multi = false) as batch op #2\n");
$update = array(
'q' => array('foo' => 'bar'),
'u' => array('$set' => array('foo' => 'def')),
'multi' => false,
);
var_dump($batch->add($update));
printf("Updating multiple documents (multi = true) as batch op #3\n");
$update = array(
'q' => array('foo' => 'bar'),
'u' => array('$set' => array('foo' => 'ghi')),
'multi' => true,
);
var_dump($batch->add($update));
printf("Upserting one document (with _id) as batch op #4\n");
$update = array(
'q' => array('_id' => 5),
'u' => array('$set' => array('x' => 2)),
'upsert' => true,
);
var_dump($batch->add($update));
printf("Upserting one document (_id unspecified) as batch op #5\n");
$update = array(
'q' => array('foo' => 'bar'),
'u' => array('$set' => array('x' => 1)),
'upsert' => true,
);
var_dump($batch->add($update));
$res = $batch->execute(array('w' => 1));
dump_these_keys($res, array('upserted', 'nMatched', 'nModified', 'nUpserted', 'ok'));
printf("Total batches sent to server: %d\n", $numBatches);
?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
Updating one document (multi unspecified) as batch op #1
bool(true)
Updating one document (multi = false) as batch op #2
bool(true)
Updating multiple documents (multi = true) as batch op #3
bool(true)
Upserting one document (with _id) as batch op #4
bool(true)
Upserting one document (_id unspecified) as batch op #5
bool(true)
array(5) {
["upserted"]=>
array(2) {
[0]=>
array(2) {
["index"]=>
int(3)
["_id"]=>
int(5)
}
[1]=>
array(2) {
["index"]=>
int(4)
["_id"]=>
object(MongoId)#%d (1) {
["$id"]=>
string(24) "%s"
}
}
}
["nMatched"]=>
int(4)
["nModified"]=>
int(4)
["nUpserted"]=>
int(2)
["ok"]=>
bool(true)
}
Total batches sent to server: 1
===DONE===
|