MongoDB\Driver\BulkWrite::insert

(mongodb >=1.0.0)

MongoDB\Driver\BulkWrite::insertAdd an insert operation to the bulk

说明

public MongoDB\Driver\BulkWrite::insert ( array|object $document ) : mixed

Adds an insert operation to the MongoDB\Driver\BulkWrite.

参数

document ( array|object)

A document to insert.

返回值

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.

错误/异常

更新日志

版本 说明
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.

范例

Example #1 MongoDB\Driver\BulkWrite::insert() example

<?php

$bulk 
= new MongoDB\Driver\BulkWrite;

$document1 = ['title' => 'one'];
$document2 = ['_id' => 'custom ID''title' => 'two'];
$document3 = ['_id' => new MongoDB\BSON\ObjectId'title' => 'three'];

$_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');
$result $manager->executeBulkWrite('db.collection'$bulk);

?>

以上例程的输出类似于:

object(MongoDB\BSON\ObjectId)#3 (1) {
  ["oid"]=>
  string(24) "54d51146bd21b91405401d92"
}
NULL
NULL
相关文章
php mongodb driver the mongodb driver bulkwrite classphp mongodb driver exception the mongodb driver exception bulkwriteexception classphp mongodb driver exception the mongodb driver exception connectionexception classphp mongodb driver monitoring the mongodb driver monitoring subscriber interfacephp mongodb driver bulkwrite create a new bulkwritephp mongodb driver bulkwrite count number of write operations in the bulkphp mongodb driver bulkwrite add a delete operation to the bulkphp mongodb driver bulkwrite add an insert operation to the bulkphp mongodb driver bulkwrite add an update operation to the bulkphp mongodb driver cursor checks if the cursor may have additional resultsphp mongodb driver manager select a server matching a read preferencephp mongodb driver monitoring commandfailedevent returns the command s operation idphp mongodb driver monitoring commandstartedevent returns the command s operation idphp mongodb driver monitoring commandsubscriber notification method for a started commandphp mongodb driver monitoring commandsucceededevent returns the command s operation idphp mongodb driver readpreference returns the readpreference s mode optionphp mongodb driver server checks if this server is an arbiter member of a replica setphp mongodb driver writeerror returns the index of the write operation corresponding to this writeerrorphp mongodb driver writeerror returns additional metadata for the writeerrorphp mongodb driver exception writeexception returns the writeresult for the failed write operation
关注编程学问公众号