(mongodb >=0.2.0)
MongoDB\Driver\BulkWrite::insert — Add a insert operation to the bulk
$document
)
document
A document to add to the insert bulk.
If the document
did not have an _id, a
BSON\ObjectID will be generated and returned.
Otherwise no value is returned.
Example #1 MongoDB\Driver\BulkWrite::insert() example
<?php
$bulk = new MongoDB\Driver\BulkWrite(true);
$document1 = array(
"title" => "Example#1",
);
$document2 = array(
"_id" => "custom pregenerated ID",
"title" => "Example#1",
);
$document3 = array(
"_id" => new BSON\ObjectID,
"title" => "Example#1",
);
$_id1 = $bulk->insert($document1);
$_id2 = $bulk->insert($document2);
$_id3 = $bulk->insert($document3);
var_dump($_id1, $_id2, $_id3);
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $manager->executeBulkWrite("databaseName.collectionName", $bulk, $writeConcern);
?>
以上例程的输出类似于:
object(BSON\ObjectID)#3 (1) { ["oid"]=> string(24) "54d51146bd21b91405401d92" } NULL NULL