I can't reproduce this using the following script:
$m = new MongoClient();
$m->test->foo->drop();
$rs = $m->test->foo->batchInsert(
[
(object) ['_id' => 1],
(object) ['_id' => 2],
],
['continueOnError' => true]
);
echo "batchInsert returned:
";
var_dump($rs);
echo "
test.foo contents:
";
var_dump(iterator_to_array($m->test->foo->find()));
This reports a successful GLE response (ok
is 1 in the $rs
variable) and ultimately prints both documents that were just inserted into the collection. Playing around with the documents themselves and continueOnError
behaved as expected. For instance, continueOnError
as false
with the following batch:
[
(object) ['_id' => 1],
(object) ['_id' => 1],
(object) ['_id' => 2],
]
...will yield an exception processing the second document. The third document never gets inserted. With continueOnError
as true
, we still get the exception but the third document does get inserted.
One additional thought would be to attempt debugging with the stream context loggers. Modify the above script by adding the following to the MongoClient:
function log_batchinsert($server, $docs, $options, $info) {
var_dump(func_get_args());
}
$ctx = stream_context_create([
'mongodb' => ['log_batchinsert' => 'log_batchinsert'],
]);
$m = new MongoClient(null, [], ['context' => $ctx]);
$m->test->foo->drop();
$rs = $m->test->foo->batchInsert(/* ... */);
This will yield some additional debug output from the driver when the batchInsert operation is sent across the wire (your results may differ slightly):
array(4) {
[0]=>
array(5) {
["hash"]=>
string(25) "localhost:27017;-;.;10494"
["type"]=>
int(1)
["max_bson_size"]=>
int(16777216)
["max_message_size"]=>
int(48000000)
["request_id"]=>
int(1276292850)
}
[1]=>
array(2) {
[0]=>
object(stdClass)#4 (1) {
["_id"]=>
int(1)
}
[1]=>
object(stdClass)#5 (1) {
["_id"]=>
int(2)
}
}
[2]=>
array(1) {
["flags"]=>
int(1)
}
[3]=>
array(1) {
["continueOnError"]=>
bool(true)
}
}
Here, the function logs some information about the server connection used, the batch array itself (notice that it picks up we're using stdClass
), and the flags for the OP_INSERT operation. The second argument (i.e. the batch) will be your best tool (short of monitoring network traffic) for seeing exactly what the driver sent over to MongoDB.