International PHP Conference Munich 2025

MongoDB\Driver\BulkWriteCommand::insertOne

(mongodb >=2.1.0)

MongoDB\Driver\BulkWriteCommand::insertOneAjoute une opération insertOne

Description

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

Ajoute une opération insertOne à la MongoDB\Driver\BulkWriteCommand. Le document sera insérer dans la collection identifiée par namespace.

Liste de paramètres

namespace (string)

Un espace de noms totalement qualifié (e.g. "databaseName.collectionName")

document (array|object)

Le document à insérer.

Valeurs de retour

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.

Erreurs / Exceptions

  • Lance une exception MongoDB\Driver\InvalidArgumentException lors d'une erreur survenue pendant l'analyse d'un argument.

Exemples

Exemple #1 Exemple de MongoDB\Driver\BulkWriteCommand::insertOne()

<?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);

?>

Résultat de l'exemple ci-dessus est similaire à :

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

Voir aussi

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top