如何将两个不同的文档插入到相同的mongo db集合中?

How can insert two different documents into same collection in mongo db using php? I tried the code below but it only inserted document1.

$NewCollection->batchInsert(
                array($document1, $document2),
                array('continueOnError' => true)
             );

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.

The first parameter:

An array of arrays or objects

(http://www.php.net/manual/en/mongocollection.batchinsert.php)

Is are $document1 and $document2 arrays or objects (and if they have a MongoID they not beeing identically) ?

If you don't continueOnError, do you get any?

If you have the latest updates of the Mongo PHP lib and the above questions don't lead to a solution you might have found a bug in the libs..