PHP 8.5.0 Alpha 1 available for testing

MongoDB\Driver\BulkWriteCommand::insertOne

(mongodb >=2.1.0)

MongoDB\Driver\BulkWriteCommand::insertOneAñade una operación insertOne

Descripción

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

Añade una operación insertOne a la MongoDB\Driver\BulkWriteCommand. El documento será insertado en la colección identificada por namespace.

Parámetros

namespace (string)

Un espacio de nombres completamente cualificado (p. ej. "nombreBaseDatos.nombreColección").

document (array|object)

El documento a insertar.

Valores devueltos

Devuelve el _id del documento insertado. Si el document no tenía un _id, el MongoDB\BSON\ObjectId generado para la inserción será devuelto.

Errores/Excepciones

Ejemplos

Ejemplo #1 Ejemplo 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);

?>

El resultado del ejemplo sería algo similar a:

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

Ver también

add a note

User Contributed Notes

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