# MongoDB\Driver\BulkWriteCommand::insertOne

(mongodb >=2.1.0)

MongoDB\Driver\BulkWriteCommand::insertOne — Add an insertOne operation

### Beschreibung

public MongoDB\Driver\BulkWriteCommand::insertOne(string `$namespace`, array|object `$document`): mixed

Add an insertOne operation to the MongoDB\Driver\BulkWriteCommand. The document will be inserted into the collection identified by `namespace`.

### Parameter-Liste

`namespace` (string)

  A fully qualified namespace (e.g. `"databaseName.collectionName"`).

`document` (array|object)

  A document to insert.

### Rückgabewerte

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.

### Fehler/Exceptions

- Wirft eine MongoDB\Driver\Exception\InvalidArgumentException, wenn beim Analysieren von Argumenten ein Fehler auftritt.

### Beispiele

Beispiel #1 MongoDB\Driver\BulkWriteCommand::insertOne() example

`<?php
$manager = new MongoDB\Driver\Manager;
$bulk = new MongoDB\Driver\BulkWriteCommand;
$doc1 = ['x' => 1];
$doc2 = ['_id' => 'custom-id', 'x' => 2];
$doc3 = ['_id' => new MongoDB\BSON\ObjectId('0123456789abcdef01234567'), 'x' => 3];
$id1 = $bulk->insertOne('db.coll', $doc1);
$id2 = $bulk->insertOne('db.coll', $doc2);
$id3 = $bulk->insertOne('db.coll', $doc3);
var_dump($id1, $id2, $id3);
$result = $manager->executeBulkWriteCommand($bulk);
?>`

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

    object(MongoDB\BSON\ObjectId)#3 (1) {
      ["oid"]=>
      string(24) "67f58058d1a0aa2fd80d55d0"
    }
    string(9) "custom-id"
    object(MongoDB\BSON\ObjectId)#4 (1) {
      ["oid"]=>
      string(24) "0123456789abcdef01234567"
    }

### Siehe auch

- MongoDB\Driver\Manager::executeBulkWriteCommand() - Execute write operations using the bulkWrite command
- MongoDB\Driver\BulkWriteCommandResult
- MongoDB\BSON\ObjectId

`↑` and `↓` to navigate • `Enter` to select • `Esc` to close • `/` to open