array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'zh', ), 'this' => array ( 0 => 'mongodb-driver-bulkwrite.insert.php', 1 => 'MongoDB\\Driver\\BulkWrite::insert', 2 => 'Add an insert operation to the bulk', ), 'up' => array ( 0 => 'class.mongodb-driver-bulkwrite.php', 1 => 'MongoDB\\Driver\\BulkWrite', ), 'prev' => array ( 0 => 'mongodb-driver-bulkwrite.delete.php', 1 => 'MongoDB\\Driver\\BulkWrite::delete', ), 'next' => array ( 0 => 'mongodb-driver-bulkwrite.update.php', 1 => 'MongoDB\\Driver\\BulkWrite::update', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'reference/mongodb/mongodb/driver/bulkwrite/insert.xml', ), 'history' => array ( ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); contributors($setup); ?>
(mongodb >=1.0.0)
MongoDB\Driver\BulkWrite::insert — Add an insert operation to the bulk
Adds an insert operation to the MongoDB\Driver\BulkWrite.
Returns the _id of the inserted document. If the
document did not have an _id, the
MongoDB\BSON\ObjectId generated for the insert will
be returned.
| 版本 | 说明 |
|---|---|
| PECL mongodb 1.3.0 |
The _id of the inserted document is always returned.
Previously, the method only returned a value if a
MongoDB\BSON\ObjectId was generated.
|
示例 #1 MongoDB\Driver\BulkWrite::insert() example
<?php
$bulk = new MongoDB\Driver\BulkWrite;
$doc1 = ['x' => 1];
$doc2 = ['_id' => 'custom-id', 'x' => 2];
$doc3 = ['_id' => new MongoDB\BSON\ObjectId('0123456789abcdef01234567'), 'x' => 3];
$id1 = $bulk->insert($doc1);
$id2 = $bulk->insert($doc2);
$id3 = $bulk->insert($doc3);
var_dump($id1, $id2, $id3);
$manager = new MongoDB\Driver\Manager('mongodb://localhost:27017');
$result = $manager->executeBulkWrite('db.collection', $bulk);
?>以上示例的输出类似于:
object(MongoDB\BSON\ObjectId)#3 (1) {
["oid"]=>
string(24) "67f58058d1a0aa2fd80d55d0"
}
string(9) "custom-id"
object(MongoDB\BSON\ObjectId)#4 (1) {
["oid"]=>
string(24) "0123456789abcdef01234567"
}